Part Number Hot Search : 
AD5542CR SA155 0C028 0603LWCT 1N248A CD261X TL084 25600
Product Description
Full Text Search
 

To Download USBB17CC78K4 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  user?s manual target devices: 78k/iv series printed in japan document no. u15556ej1v0um00 (1st edition) date published november 2001 n cp(k) cc78k4 ver.2.30 or later c compiler language ? 2001
user?s manual u15556ej1v0um 2 [memo]
user?s manual u15556ej1v0um 3 windows is either a registered trademark or a trademark of microsoft corporation in the united states and/or other countries. pc/at is a trademark of international business machines corporation. unix is a registered trademark licensed by x/open company limited in the us and other countries. sparcstation is a trademark of sparc international, inc. hp9000 series 700 is a trademark of hewlett-packard company. m8e 00. 4 the information in this document is current as of july, 2001. the information is subject to change without notice. for actual design-in, refer to the latest publications of nec's data sheets or data books, etc., for the most up-to-date specifications of nec semiconductor products. not all products and/or types are available in every country. please check with an nec sales representative for availability and additional information. no part of this document may be copied or reproduced in any form or by any means without prior written consent of nec. nec assumes no responsibility for any errors that may appear in this document. nec does not assume any liability for infringement of patents, copyrights or other intellectual property rights of third parties by or arising from the use of nec semiconductor products listed in this document or any other liability arising from the use of such products. no license, express, implied or otherwise, is granted under any patents, copyrights or other intellectual property rights of nec or others. descriptions of circuits, software and other related information in this document are provided for illustrative purposes in semiconductor product operation and application examples. the incorporation of these circuits, software and information in the design of customer's equipment shall be done under the full responsibility of customer. nec assumes no responsibility for any losses incurred by customers or third parties arising from the use of these circuits, software and information. while nec endeavours to enhance the quality, reliability and safety of nec semiconductor products, customers agree and acknowledge that the possibility of defects thereof cannot be eliminated entirely. to minimize risks of damage to property or injury (including death) to persons arising from defects in nec semiconductor products, customers must incorporate sufficient safety measures in their design, such as redundancy, fire-containment, and anti-failure features. nec semiconductor products are classified into the following three quality grades: "standard", "special" and "specific". the "specific" quality grade applies only to semiconductor products developed based on a customer-designated "quality assurance program" for a specific application. the recommended applications of a semiconductor product depend on its quality grade, as indicated below. customers must check the quality grade of each semiconductor product before using it in a pa rticular application. "standard": computers, office equipment, communications equipment, test and measurement equipment, audio and visual equipment, home electronic appliances, machine tools, personal electronic equipment and industrial robots "special": transportation equipment (automobiles, trains, ships, etc.), traffic control systems, anti-disaster systems, anti-crime systems, safety equipment and medical equipment (not specifically designed for life support) "specific": aircraft, aerospace equipment, submersible repeaters, nuclear reactor control systems, life support systems and medical equipment for life support, etc. the quality grade of nec semiconductor products is "standard" unless otherwise expressly specified in nec's data sheets or data books, etc. if customers wish to use nec semiconductor products in applications not intended by nec, they must contact an nec sales representative in advance to determine nec's willingness to support a given application. (note) (1) "nec" as used in this statement means nec corporation and also includes its majority-owned subsidiaries. (2) "nec semiconductor products" means any semiconductor product developed or manufactured by or for nec (as defined above). ? ? ? ? ? ?
user ? s manual u15556ej1v0um 4 regional information some information contained in this document may vary from country to country. before using any nec product in your application, piease contact the nec office in your country to obtain a list of authorized representatives and distributors. they will verify: ? device availability ? ordering information ? product release schedule ? availability of related technical literature ? development environment specifications (for example, specifications for third-party tools and components, host computers, power plugs, ac supply voltages, and so forth) ? network requirements in addition, trademarks, registered trademarks, export restrictions, and other legal issues may also vary from country to country. nec electronics inc. (u.s.) santa clara, california tel: 408-588-6000 800-366-9782 fax: 408-588-6130 800-729-9288 nec electronics (germany) gmbh duesseldorf, germany tel: 0211-65 03 02 fax: 0211-65 03 490 nec electronics (uk) ltd. milton keynes, uk tel: 01908-691-133 fax: 01908-670-290 nec electronics italiana s.r.l. milano, italy tel: 02-66 75 41 fax: 02-66 75 42 99 nec electronics (germany) gmbh benelux office eindhoven, the netherlands tel: 040-2445845 fax: 040-2444580 nec electronics (france) s.a. velizy-villacoublay, france tel: 01-3067-5800 fax: 01-3067-5899 nec electronics (france) s.a. madrid office madrid, spain tel: 091-504-2787 fax: 091-504-2860 nec electronics (germany) gmbh scandinavia office taeby, sweden tel: 08-63 80 820 fax: 08-63 80 388 nec electronics hong kong ltd. hong kong tel: 2886-9318 fax: 2886-9022/9044 nec electronics hong kong ltd. seoul branch seoul, korea tel: 02-528-0303 fax: 02-528-4411 nec electronics singapore pte. ltd. novena square, singapore tel: 253-8311 fax: 250-3583 nec electronics taiwan ltd. taipei, taiwan tel: 02-2719-2377 fax: 02-2719-5951 nec do brasil s.a. electron devices division guarulhos-sp, brasil tel: 11-6462-6810 fax: 11-6462-6829 j01.2
user?s manual u15556ej1v0um 5 introduction the cc78k4 c compiler (hereafter referred to as this c compiler) was developed based on chapter 2 environment and chapter 3 language in the draft proposed american national standard for information systems - programming language c (december 7, 1988). therefore, by compiling c source programs conforming to the ansi standard with this c compiler, 78k/iv series application products can be developed. the cc78k4 c compiler language (this manual) has been prepared to give those who develop software by using this c compiler a correct understanding of the basic functions and language specifications of this c compiler. this manual does not cover how to operate this c compiler. therefore, after you have comprehended the contents of this manual, read the cc78k4 c compiler operation user?s manual (u15557e) . for the architecture of 78k/iv series, refer to the user ? s manual of each product of 78k/iv series.
user?s manual u15556ej1v0um 6 [target devices] software for the 78k/iv series microcontrollers can be developed with this c compiler. note that the device file (sold separately) corresponding to the target device is necessary. [target readers] although this manual is intended for those who have read the user's manual of the microcontroller subject to software development and have experience in software programming, the reader need not necessarily have knowledge of c compilers or c language. discussions in this manual assume that readers are familiar with software terminology. [organization] this manual consists of the following 13 chapters and appendixes. chapter 1 general outlines the general functions of c compilers and the performance characteristics and features of this c compiler. chapter 2 constructs of c language explains the constituent elements of a c source module file. chapter 3 declaration of types and storage classes explains the data types and storage classes used in c and how to declare the type and storage class of a data object or function. chapter 4 type conversions explains the conversions of data types to be automatically carried out by this c compiler. chapter 5 operators and expressions describes the operators and expressions that can be used in c and the priority of operators. chapter 6 control structures of c language explains the program control structures of c and the statements to be executed in c. chapter 7 structures and unions explains the concept of structures and unions and how to refer to structure and union members. chapter 8 external definitions describes the types of external definitions and how to use external declarations. chapter 9 preprocessing directives details the types of preprocessing directives and how to use each preprocessing directive. chapter 10 library functions details the types of c library functions and how to use each library function. chapter 11 extended functions explains the extended functions of this c compiler provided to make the most of the target device. chapter 12 referencing between c and assembler describes the method of linking a c source program with a program written in assembly language. chapter 13 effective utilization outlines how to effectively use this c compiler. appendixes a through e contain a list of labels for saddr area, a list of segment names, a list of runtime libraries, a list of library stack consumption, and an index for quick reference.
user?s manual u15556ej1v0um 7 [how to read this manual]  for those who are not familiar with c compilers or c language: read from chapter 1 , as this manual covers from the program control structures of c to the extended functions of this c compiler. in chapter 1 , an example of c source program is used to show where in the manual details can be referenced.  for those who are familiar with c compilers or c language: the language specifications of this c compiler conform to ansi standard c . therefore, you may start from chapter 11, which explains the extended functions unique to this c compiler. when reading chapter 11 , also refer to the user's manual supplied with the target device in the 78k/iv series, if necessary. [related documents] document name document no. cc78k4 c compiler operation user?s manual u15557e [reference] draft proposed american national standard for information systems - programming language c (december 7, 1988) [terms] rtos = 78k/iv series real-time os rx78k/iv [conventions] the following conventions are used in this manual. symbol meaning ... continuation (repetition) of data in the same format ? ? characters enclosed in a pair of double quotes must be input as is. ? ? characters enclosed in a pair of single quotes must be input as is. : this part of the program description is omitted. / delimiter \ backslash [ ] parameters in square brackets may be omitted.
user?s manual u15556ej1v0um 8 contents chapter 1 general ........................................................................................................... .......... 19 1.1 c language and assembly language ............................................................................... 19 1.2 program development procedure by c compiler............................................................. 21 1.3 basic structure of c source program................................................................................ 23 1.3.1 program format............................................................................................................ ............ 23 1.4 reminders before program development ......................................................................... 26 1.5 features of this c compiler ............................................................................................... 28 <1> callt /_ _ callt functions ............................................................................................................... 28 <2> register variables .......................................................................................................... ............ 28 <3> using the saddr area................................................................................................................. 29 <4> sfr area .......................................................................................................................... ............ 29 <5> noauto functions ..................................................................................................................... .. 29 <6> norec /_ _ leaf functions ............................................................................................................. 29 <7> bit type variables and boolean /_ _boolean type variables ...................................................... 29 <8> boolean1 type variables............................................................................................................ 29 <9> asm statements.............................................................................................................. ........... 29 <10> interrupt functions ........................................................................................................ .............. 29 <11> interrupt function qualifier ............................................................................................... ........... 29 <12> interrupt function......................................................................................................... ............... 30 <13> cpu control instructions ................................................................................................... ......... 30 <14> callf /_ _ callf function................................................................................................................. 30 <15> usage of 16 mb expansion space ............................................................................................. 30 <16> location function.......................................................................................................... .............. 30 <17> absolute address access function ........................................................................................... .. 30 <18> bit field declaration ...................................................................................................... .............. 30 <19> function to change compiler output section name .................................................................... 30 <20> binary constant description function ....................................................................................... ... 30 <21> module name change functions ............................................................................................... .. 30 <22> rotate function............................................................................................................ ............... 30 <23> multiplication function .................................................................................................... ............ 30 <24> division function.......................................................................................................... ............... 30 <25> data insertion function .................................................................................................... ........... 31 <26> interrupt handler for rtos ................................................................................................. ....... 31 <27> interrupt handler qualifier for rtos....................................................................................... .... 31 <28> task function for rtos ..................................................................................................... ........ 31 <29> changing function call interface........................................................................................... ...... 31 <30> change of calculation method of offset of arrays and pointers.................................................. 31 <31> pascal function (_ _pascal )....................................................................................................... 31 <32> automatic pascal functionization of function call interface ......................................................... 31 <33> flash area allocation method............................................................................................... ...... 31 <34> flash area branch table .................................................................................................... ......... 31 <35> function call function from boot area to flash area.................................................................... 31 <36> firmware rom function ...................................................................................................... ....... 31 <37> limiting int expansion of argument/return value........................................................................ 32 <38> memory manipulation function ............................................................................................... .... 32
user?s manual u15556ej1v0um 9 <39> callf two-step branch function ................................................................................................... 32 <40> automatic callf functionization of function call interface ............................................................ 32 <41> three-byte address reference/generation function.................................................................... 32 <42> absolute address allocation specification.................................................................................. 32 chapter 2 constructs of c language............................................................................33 2.1 character sets.............................................................................................................. .........34 (1) character sets .............................................................................................................. ................ 34 (2) escape sequences ............................................................................................................ ........... 35 (3) trigraph sequences.......................................................................................................... ............ 35 2.2 keywords .................................................................................................................... ...........36 (1) ansi keywords ............................................................................................................... .............. 36 (2) keywords added for the cc78k4 ............................................................................................... .. 36 2.3 identifiers ................................................................................................................. ..............37 2.3.1 scope of identifiers...................................................................................................... ............ 37 (1) function scope .............................................................................................................. .38 (2) file scope .................................................................................................................. ..... 38 (3) block scope ................................................................................................................. ... 38 (4) function prototype scope ............................................................................................... 38 2.3.2 linkage of identifiers .................................................................................................... ........... 39 (1) external linkage............................................................................................................ .. 39 (2) internal linkage ............................................................................................................ ... 39 (3) no linkage .................................................................................................................. .... 39 2.3.3 name space for identifiers................................................................................................ ....... 39 2.3.4 storage duration of objects ............................................................................................... ...... 39 (1) static storage duration ................................................................................................... 39 (2) automatic storage duration ............................................................................................ 40 2.3.5 data types ................................................................................................................ ............... 40 (1) basic types ................................................................................................................. .... 41 (2) character types ............................................................................................................. .44 (3) incomplete types ............................................................................................................ 45 (4) derived types ............................................................................................................... .. 45 (5) scalar types................................................................................................................ .... 45 2.3.6 compatible type and composite type ...................................................................................... 46 (1) compatible type ............................................................................................................. 46 (2) composite type .............................................................................................................. 46 2.4 constants................................................................................................................... ............46 2.4.1 floating-point constant ................................................................................................... ......... 47 2.4.2 integer constant.......................................................................................................... ............. 47 (1) decimal constant............................................................................................................ 47 (2) octal constant .............................................................................................................. .. 47 (3) hexadecimal constant .................................................................................................... 47 2.4.3 enumeration constants..................................................................................................... ....... 48 2.4.4 character constants ....................................................................................................... ......... 48 2.5 string literals ............................................................................................................. ...........49 2.6 operators ................................................................................................................... ............49 2.7 delimiters.................................................................................................................. .............49 2.8 header name ................................................................................................................. ........50
user?s manual u15556ej1v0um 10 2.9 comment..................................................................................................................... .......... 50 chapter  3  declaration of types and storage classes....................................... 51 3.1 storage class specifiers .................................................................................................... .52 (1) typedef..................................................................................................................... ..................... 52 (2) extern...................................................................................................................... ...................... 52 (3) static ...................................................................................................................... ....................... 52 (4) auto........................................................................................................................ ....................... 52 (5) register.................................................................................................................... ...................... 52 3.2 type specifiers............................................................................................................. ........ 53 3.2.1 structure specifier and union specifier ................................................................................... .55 (1) structure specifier......................................................................................................... .. 55 (2) union specifier............................................................................................................. ... 55 (3) bit field................................................................................................................... ......... 56 3.2.2 enumeration specifiers .................................................................................................... ........ 56 3.2.3 tags...................................................................................................................... ................... 57 3.3 type qualifiers ............................................................................................................. ........ 58 3.4 declarators................................................................................................................. ........... 59 3.4.1 pointer declarators ....................................................................................................... ........... 59 3.4.2 array declarators ......................................................................................................... ............ 59 3.4.3 function declarators (including prototype declarations) .......................................................... 60 3.5 type names .................................................................................................................. ........ 60 3.6 typedef declarations ........................................................................................................ .... 60 3.7 initialization.............................................................................................................. ............. 62 (1) initialization of objects which have a static storage duration ........................................................ 62 (2) initialization of objects that have an automatic storage duration .................................................. 62 (3) initialization of character arrays.......................................................................................... .......... 62 (4) initialization of aggregate or union type objects ........................................................................... 63 chapter  4  type conversions ................................................................................................ 65 4.1 arithmetic operands......................................................................................................... ... 67 (1) characters and integers (general integral promotion) .................................................................. 67 (2) signed integers and unsigned integers ....................................................................................... .67 (3) usual arithmetic type conversions........................................................................................... ..... 68 4.2 other operands .............................................................................................................. ...... 69 (1) left-side values and function locators ...................................................................................... .... 69 (2) void ........................................................................................................................ ....................... 69 (3) pointers .................................................................................................................... .................... 69 chapter 5 operators and expressions .......................................................................... 70 5.1 primary expressions......................................................................................................... ... 73 5.2 postfix operators ........................................................................................................... ...... 73 (1) subscript operators ......................................................................................................... ............. 74 (2) function call operators ..................................................................................................... ............ 75 (3) structure and union member .................................................................................................. ...... 76 (4) postfix increment/decrement operators ....................................................................................... 78 5.3 unary operators ............................................................................................................. ...... 79 (1) prefix increment and decrement operators.................................................................................. 80
user?s manual u15556ej1v0um 11 (2) address and indirection operators ........................................................................................... .... 81 (3) unary arithmetic operators (+ ? ~ !) ........................................................................................ ..... 82 (4) sizeof operators............................................................................................................ ................ 83 5.4 cast operators .............................................................................................................. ........84 5.5 arithmetic operators ........................................................................................................ ....85 (1) multiplicative operators.................................................................................................... ............. 86 (2) additive operators .......................................................................................................... .............. 87 5.6 bitwise shift operators ..................................................................................................... ...88 5.7 relational operators........................................................................................................ .....90 (1) relational operators ........................................................................................................ ............. 91 (2) equality operators .......................................................................................................... .............. 92 5.8 bitwise logical operators................................................................................................... .93 (1) bitwise and operators ....................................................................................................... .......... 94 (2) bitwise xor operators ....................................................................................................... .......... 95 (3) bitwise inclusive or operators .............................................................................................. ...... 96 5.9 logical operators ........................................................................................................... ......97 (1) logical and operators ....................................................................................................... .......... 98 (2) logical or operators ........................................................................................................ ........... 99 5.10 conditional operators ...................................................................................................... ..100 5.11 assignment operators ....................................................................................................... 101 (1) simple assignment operators ................................................................................................. .... 102 (2) compound assignment operators .............................................................................................. 1 03 5.12 comma operator............................................................................................................. ....104 5.13 constant expressions ....................................................................................................... .105 (1) general integral constant expression........................................................................................ . 105 (2) arithmetic constant expression .............................................................................................. .... 105 (3) address constant expression ................................................................................................. .... 105 chapter 6 control structures of c language ......................................................106 6.1 labeled statements .......................................................................................................... ..108 (1) case label .................................................................................................................. ................. 109 (2) default label ............................................................................................................... ................. 111 6.2 compound statements or blocks .....................................................................................112 6.3 expression statements and null statements...................................................................112 6.4 conditional statements...................................................................................................... 113 (1) if and if ... else statements............................................................................................... ........... 114 (2) switch statement............................................................................................................ ............. 115 6.5 iteration statements........................................................................................................ ....116 (1) while statement ............................................................................................................. ............. 117 (2) do statement................................................................................................................ ............... 118 (3) for statement ............................................................................................................... ............... 119 6.6 branch statements ........................................................................................................... ..120 (1) goto statement.............................................................................................................. .............. 121 (2) continue statement .......................................................................................................... ........... 122 (3) break statement............................................................................................................. ............. 123 (4) return statement ............................................................................................................ ............. 124
user?s manual u15556ej1v0um 12 chapter 7 structures and unions .................................................................................... 125 7.1 structures .................................................................................................................. ......... 126 (1) declaration of structure and structure variable........................................................................... 12 6 (2) structure declaration list .................................................................................................. ........... 127 (3) arrays and pointers ......................................................................................................... ........... 128 (4) how to refer to structure members ........................................................................................... .. 129 7.2 unions ...................................................................................................................... ........... 130 (1) declaration of union and union variable ..................................................................................... 130 (2) union declaration list ...................................................................................................... ............ 130 (3) union arrays and pointers ................................................................................................... ....... 131 (4) how to refer to union members ............................................................................................... ... 132 chapter 8 external definitions ........................................................................................ 133 8.1 function definition......................................................................................................... .... 134 8.2 external object definitions ............................................................................................... 13 6 chapter 9 preprocessing directives (compiler directives) .............................. 137 9.1 conditional translation directives ................................................................................... 137 (1) #if directive ............................................................................................................... .................. 138 (2) #elif directive............................................................................................................. .................. 139 (3) #ifdef directive ............................................................................................................ ................ 140 (4) #ifndef directive ........................................................................................................... ............... 141 (5) #else directive............................................................................................................. ................ 142 (6) #endif directive ............................................................................................................ ............... 143 9.2 source file inclusion directive ......................................................................................... 144 (1) #include < > ............................................................................................................... ................ 145 (2) #include ? ?............................................................................................................... .................. 146 (3) #include preprocessing token string ......................................................................................... .. 147 9.3 macro replacement directives ......................................................................................... 148 (1) actual argument replacement................................................................................................. .... 148 (2) # operator .................................................................................................................. ................. 148 (3) ## operator ................................................................................................................. ................ 148 (4) re-scanning and further replacement ........................................................................................ 1 49 (5) scope of macro definition ................................................................................................... ........ 149 (6) #define directive ........................................................................................................... .............. 150 (7) #define( ) directive ....................................................................................................... .............. 151 (8) #undef directive ............................................................................................................ .............. 152 9.4 line control directive ...................................................................................................... .. 153 (1) to change the line number ................................................................................................... ...... 153 (2) to change the line number and the file name ............................................................................ 153 (3) to change using preprocessing token string.............................................................................. 153 9.5 #error preprocessing directive......................................................................................... 154 9.6 #pragma directives .......................................................................................................... .. 155 9.7 null directives ............................................................................................................. ....... 155 9.8 compiler-defined macro names ....................................................................................... 156
user?s manual u15556ej1v0um 13 chapter 10 library functions ............................................................................................158 10.1 interface between functions .............................................................................................159 10.1.1 arguments ................................................................................................................ ............. 159 10.1.2 return values ............................................................................................................ ............ 160 10.1.3 saving registers to be used by individual libraries ................................................................ 160 (1) when -zr option is not specified ................................................................................. 160 (2) when -zr option is specified ....................................................................................... 162 10.2 headers .................................................................................................................... ............163 (1) ctype.h..................................................................................................................... ................... 163 (2) setjmp.h.................................................................................................................... .................. 163 (3) stdarg.h .................................................................................................................... .................. 163 (4) stdio.h..................................................................................................................... .................... 164 (5) stdlib.h .................................................................................................................... .................... 164 (6) string.h.................................................................................................................... .................... 165 (7) error.h..................................................................................................................... .................... 165 (8) errno.h ..................................................................................................................... ................... 165 (9) limits.h .................................................................................................................... .................... 165 (10) stddef.h................................................................................................................... .................... 166 (11) math.h ..................................................................................................................... ................... 166 (12) float.h.................................................................................................................... ...................... 167 (13) assert.h................................................................................................................... .................... 169 10.3 re-entrantability.......................................................................................................... ........169 (1) functions that cannot be re-entranced....................................................................................... 169 (2) functions that use the area secured in the startup routine ........................................................ 169 (3) functions that deal with floating-point numbers ......................................................................... 169 10.4 standard library functions ...............................................................................................17 0 10.5 batch files for update of startup routine and library functions ................................279 10.5.1 using batch files ........................................................................................................ ............ 280 chapter 11 extended functions.........................................................................................283 11.1 macro names................................................................................................................ .......284 11.2 keywords ................................................................................................................... ..........284 (1) functions................................................................................................................... ................. 285 (2) variables ................................................................................................................... ................. 286 11.3 memory ..................................................................................................................... ...........287 (1) memory model................................................................................................................ ............ 287 (2) register bank ............................................................................................................... .............. 287 (3) location function ........................................................................................................... ............. 287 (4) memory space ................................................................................................................ ............ 288 11.4 #pragma directives ......................................................................................................... ....289 11.5 how to use extended functions .......................................................................................291 (1) callt functions............................................................................................................. ................. 292 (2) register variables.......................................................................................................... ............. 295 (3) how to use the saddr area ................................................................................................... ...... 301 (4) how to use the sfr area ..................................................................................................... ......... 309 (5) noauto function............................................................................................................. .............. 312 (6) norec function.............................................................................................................. ............... 318 (7) bit type variables .......................................................................................................... .............. 326
user?s manual u15556ej1v0um 14 (8) _ _boolean1 type variables.................................................................................................. ....... 331 (9) asm statements .............................................................................................................. ........... 336 (10) interrupt functions........................................................................................................ ............... 340 (11) interrupt function qualifier (_ _interrupt, _ _interrupt_brk) .......................................................... 346 (12) interrupt functions........................................................................................................ ............... 349 (13) cpu control instruction .................................................................................................... ........... 352 (14) callf functions............................................................................................................ .................. 356 (15) 16 mb expansion space utilization .......................................................................................... ... 358 (16) allocation function ........................................................................................................ .............. 361 (17) absolute address access function ........................................................................................... ... 363 (18) bit field declaration ...................................................................................................... ............... 367 (19) changing compiler output section name .................................................................................... 37 5 (20) binary constant............................................................................................................ ............... 389 (21) module name changing function.............................................................................................. ... 391 (22) rotate function ............................................................................................................ ............... 392 (23) multiplication function .................................................................................................... ............. 395 (24) division function .......................................................................................................... ............... 398 (25) data insertion function.................................................................................................... ............ 400 (26) interrupt handler for real-time os (rtos).................................................................................. 402 (27) interrupt handler qualifier for real-time os (rtos) .................................................................... 408 (28) task function for real-time os (rtos)...................................................................................... . 410 (29) changing function call interface ........................................................................................... ...... 413 (30) changing the method of calculating the offset of arrays and pointers........................................ 414 (31) pascal function ............................................................................................................ ............... 421 (32) automatic pascal functionization of the function call interface ................................................... 424 (33) flash area allocation method ............................................................................................... ...... 425 (34) flash area branch table.................................................................................................... .......... 426 (35) function call function from the boot area to the flash area......................................................... 430 (36) firmware rom function...................................................................................................... ........ 433 (37) method of int expansion limitation of argument/return value ...................................................... 434 (38) memory manipulation function............................................................................................... ..... 436 (39) callf two-step branch function ............................................................................................. ........ 441 (40) automatic callf functionization of function call interface ............................................................. 444 (41) three-byte address reference/generation function..................................................................... 445 (42) absolute address allocation specification.................................................................................. . 448 11.6 modifications of c source ................................................................................................. 4 52 11.7 function call interface.................................................................................................... ... 453 11.7.1 return value ............................................................................................................. ............. 454 11.7.2 ordinary function call interface ......................................................................................... ..... 455 (1) passing arguments....................................................................................................... 455 (2) location and order of storing arguments...................................................................... 456 (3) location and order of storing automatic variables........................................................ 458 11.7.3 noauto function call interface........................................................................................... ...... 460 (1) passing arguments....................................................................................................... 460 (2) location and order of storing arguments...................................................................... 460 (3) location and order of storing automatic variables........................................................ 461 11.7.4 norec function call interface............................................................................................ ....... 463 (1) passing arguments....................................................................................................... 463
user?s manual u15556ej1v0um 15 (2) location and order of storing arguments...................................................................... 463 (3) location and order of storing automatic variables........................................................ 465 11.7.5 pascal function call interface........................................................................................... ...... 467 chapter 12 referencing the assembler .......................................................................470 12.1 accessing arguments/automatic variables ....................................................................471 12.2 storing return values ...................................................................................................... ..474 12.3 calling an assembly language routine from c..............................................................475 (1) calling an assembly language routine function (c source) ........................................................ 475 (2) saving and restoring the information of assembly language routine (assembler source) ......... 476 12.4 calling c language routine from assembly language routine ..................................479 (1) calling a c language function from assembly language (assembler source)............................. 479 12.5 referencing variables defined by other languages ......................................................481 (1) how to refer to c-defined variables ......................................................................................... ... 481 (2) how to refer to assembler-defined variables from c .................................................................. 482 12.6 other important hints...................................................................................................... ...483 (1) ?_? (underscore)............................................................................................................ .............. 483 (2) placement of arguments on the stack ........................................................................................ 4 83 chapter 13 effective utilization of compiler...........................................................484 13.1 efficient coding........................................................................................................... ........484 (1) using external variables .................................................................................................... ......... 485 (2) 1-bit data .................................................................................................................. .................. 485 (3) function definitions ........................................................................................................ ............ 486 (4) optimization option......................................................................................................... ............ 486 (5) using extended functions .................................................................................................... ....... 487 appendix a list of labels for saddr area...................................................................490 a.1 arguments of norec functions..........................................................................................490 a.2 automatic variables of norec functions ..........................................................................491 a.3 register variables.......................................................................................................... .....491 appendix b list of segment names ..................................................................................492 b.1 list of segment names ......................................................................................................4 94 b.1.1 program area and data area ................................................................................................ . 494 b.1.2 flash memory area ......................................................................................................... ...... 498 b.2 location of segment ......................................................................................................... .500 b.3 example of c source ......................................................................................................... .501 b.4 example of output assembler module .............................................................................502 appendix c list of runtime libraries .............................................................................505 appendix d list of library stack consumption .......................................................510 appendix e index ............................................................................................................ .............517
user?s manual u15556ej1v0um 16 list of figures 
   1-1 flow of compilation ......................................................................................................... ........................ 20 1-2 program development procedure by this c compiler............................................................................ 22 4-1 usual arithmetic type conversions........................................................................................... .............. 68 6-1 control flows of conditional statements..................................................................................... .......... 113 6-2 control flows of iteration statements....................................................................................... ............. 116 6-3 control flows of branch statements .......................................................................................... ........... 120 10-1 stack area when function is called (no ?zr specified) ..................................................................... 1 61 10-2 stack area when function is called (?zr specified) ......................................................................... .. 162 10-3 syntax of format commands .................................................................................................. .............. 181 10-4 syntax of input format commands ............................................................................................ ........... 185 11-1 bit allocation by bit field declaration (example 1)........................................................................ ........ 369 11-2 bit allocation by bit field declaration (example 2) ....................................................................... ........ 370 11-3 bit allocation by bit field declaration (example 3) ....................................................................... ........ 372 12-1 stack area after call ...................................................................................................... ....................... 475 12-2 stack area after return.................................................................................................... ..................... 478 12-3 calling assembly language routine from c ................................................................................... ...... 478 12-4 placing arguments of stack................................................................................................. .................. 480 12-5 placement of arguments on stack ............................................................................................ ............ 483
user?s manual u15556ej1v0um 17 list of tables (1/2)  
   1-1 maximum performance characteristics of this c compiler .................................................................... 26 2-1 list of escape sequences.................................................................................................... ................... 35 2-2 list of trigraph sequence ................................................................................................... .................... 35 2-3 list of basic data types.................................................................................................... ...................... 42 2-4 exponent relationships...................................................................................................... ..................... 43 2-5 list of operation exceptions ................................................................................................ ................... 44 4-1 list of conversions between types ........................................................................................... ............. 66 4-2 conversions from signed integral type to unsigned integral type ........................................................ 67 5-1 evaluation precedence of operators.......................................................................................... ............. 72 5-2 signs of division/remainder division operation result....................................................................... ... 85 5-3 shift operations............................................................................................................ ........................... 88 5-4 bitwise and operation....................................................................................................... ..................... 94 5-5 bitwise xor operation...................................................................................................... ...................... 95 5-6 bitwise or operation ........................................................................................................ ...................... 96 5-7 logical and operation....................................................................................................... ..................... 98 5-8 logical or operation ........................................................................................................ ...................... 99 10-1 list of passing first argument............................................................................................. .................. 159 10-2 list of storing return value............................................................................................... .................... 160 10-3 batch files for updating library functions ................................................................................. .......... 279 11-1 list of added keywords..................................................................................................... .................... 285 11-2 memory model............................................................................................................... ........................ 287 11-3 utilization of memory space................................................................................................ .................. 288 11-4 list of #pragma directives ................................................................................................. .................... 290 11-5 number of callt attribute functions that can be used when ? ql option is specified........................ 293 11-6 restriction on callt function usage........................................................................................ ............... 293 11-7 registers to allocate register variables ................................................................................... ............ 296 11-8 restrictions on register variables usage ................................................................................... .......... 297 11-9 restrictions on sreg variable usage ........................................................................................ ............. 302
user?s manual u15556ej1v0um 18 list of tables (2/2)  
   11-10 variables allocated to saddr2 area by -rd option.......................................................................... ...... 304 11-11 variables allocated to saddr2 area by -rs option.......................................................................... ...... 305 11-12 restrictions on sreg1 variable usage ...................................................................................... ............. 307 11-13 registers used for noauto function arguments (with -zo) .................................................................. 3 12 11-14 registers used for noauto function arguments (without -zo) ............................................................. 313 11-15 restrictions on noauto function arguments (with -zo) ...................................................................... .. 315 11-16 restrictions on noauto function arguments and automatic variables (without -zo) ........................... 315 11-17 registers used for norec function arguments: passing side (without -zo) ........................................ 319 11-18 registers used for norec function arguments: receiving side (without -zo) ..................................... 320 11-19 restrictions on norec function arguments (when -zo is specified) .................................................... 321 11-20 restrictions on norec function arguments (when -zo is not specified).............................................. 322 11-21 restrictions on norec function automatic variables (when -zo is not specified) ............................... 323 11-22 operators that use only constants 0 or 1 (when using bit type variable) ........................................ 327 11-23 number of usable bit type variables ....................................................................................... ............. 328 11-24 operators that use only constants 0 or 1 (when using bit type variables) ...................................... 332 11-25 number of usable _ _boolean1 type variables ............................................................................... ..... 333 11-26 save/restore area when interrupt function is used......................................................................... ... 341 11-27 storage location of return values......................................................................................... ............... 454 11-28 location where first argument is passed (on function call side)...................................................... 455 11-29 list of storing arguments (on function definition side, when -zo is not specified)........................... 456 11-30 list of storing arguments (on function definition side, when -zo is specified) ................................. 457 11-31 list of registers passing/receiving norec arguments (when -zo is not specified)............................ 464 12-1 passing arguments (function call side) ..................................................................................... .......... 471 12-2 list of storing arguments/automatic variables (inside called function) .............................................. 472 12-3 storage location of return values.......................................................................................... .............. 474 c-1 list of runtime libraries .................................................................................................. ..................... 505 d-1 list of standard library stack consumption ................................................................................. ....... 510 d-2 list of runtime library stack consumption .................................................................................. ........ 514
user?s manual u15556ej1v0um 19 chapter 1 general the cc78k4 c compiler is a language processing program that converts a source program written in the c language for the 78k/iv series or ansi-c into machine language. by the cc78k4 c compiler, object files or assembler source files for the 78k/iv series can be obtained. 1.1 c language and assembly language to have a microcontroller do its job, programs and data are necessary. these programs and data must be written by a human being (programmer) and stored in the memory section of the microcontroller. programs and data that can be handled by the microcontroller are nothing but a set or combination of binary numbers that is called machine language. an assembly language is a symbolic language characterized by one-to-one correspondence of its symbolic (mnemonic) statements with machine language instructions. because of this one-to-one correspondence, the assembly language can provide the computer with detailed instructions (for example, to improve i/o processing speed). however, this means that the programmer must instruct each and every operation of the computer. for this reason, it is difficult for him or her to understand the logic structure of the program at a glance, increasing the likelihood of to make errors in coding. high-level languages were developed as substitutes for such assembly languages. the high-level languages include a language called c that allows the programmer to write a program without regard to the architecture of the computer. compared with assembly language programs, it can be said that programs written in c have an easy-to- understand logic structure. c has a rich set of parts called functions for use in creating programs. in other words, the programmer can write a program by combining these functions.
chapter 1 general user?s manual u15556ej1v0um 20 c is characterized by its ease of understanding by human beings. however, understanding of languages by the microcontroller cannot be extended up to a program written in c. therefore, to have the computer understand the c language program, another program is required to translate c language statements into the corresponding machine language instructions. a program that translates the c language into machine language is called a c compiler. this c compiler accepts c source modules as inputs and generates object modules or assembler source modules as outputs. therefore, the programmer can write a program in c and if he or she wishes to instruct the computer up to details of program execution, the c source program can be modified in assembly language. the flow of translation by this c compiler is illustrated in figure 1-1. figure 1-1. flow of compilation program written in c language (c source module file) translation program (compiler) program coded in a set of binary numbers (object module file) translation program (assembler) program coded in a set of binary numbers (object module file) (assembler source module file)
chapter 1 general user ? s manual u15556ej1v0um 21 1.2 program development procedure by c compiler product (program) development by the c compiler requires a linker, which unites together object module files created by the compiler, a librarian, which creates library files, and a debugger, which locates and corrects bugs (errors or mistakes) in each created c source program. the software required in connection with this c compiler is shown below.  editor .......................................... for source module file creation  ra78k4 assembler package assembler .................................. for converting assembly language into machine language object converter ......................... for conversion to hex-format object module files linker.......................................... for linking object module files librarian ..................................... for creating library files  debugger (for 78k/iv) ................ for debugging c source module files the product development procedure by the c compiler is as shown below. <1> divides the product into functions. <2> creates a c source module for each function. <3> translates each c source module. <4> registers the modules to be used frequently in the library. <5> links object module files. <6> debugs each module. <7> converts object modules into hex-format object files. as mentioned earlier, this c compiler translates (compiles) a c source module file and creates an object module file or assembler source module file. by manually optimizing the created assembler source module file and embedding it into the c source, efficient object modules can be created. this is useful when high-speed processing is a must or when modules must be made compact.
chapter 1 general user?s manual u15556ej1v0um 22 figure 1-2. program development procedure by this c compiler librarian assembler structured assembler c compiler real-time os linker list converter object converter structured assembler source c source include file assembler source assemble list object module file library file load module file dedicated parallel interface/rs-232-c rs-232-c hex-format object absolute assemble list prom programmer integrated debugger system simulator in-circuit emulator assembler source library file
chapter 1 general user ? s manual u15556ej1v0um 23 1.3 basic structure of c source program 1.3.1 program format a c language program is a collection of functions. these functions must be created so that they have independent special-purpose or characteristic actions. all c language programs must have a function main ( ) which becomes the main routine in c and is the first function that is called when execution begins. each function consists of a header part, which defines its function name and arguments, and a body part, which consists of declarations and statements. the format of c programs is shown below. definition of variables/constants definition of each data, variable, and macro instruction main (arguments) header of function main ( ) { statement1; statement2; function1 (arguments); body of function main ( ) function2 (arguments); } function1 (arguments) { statement1; function 1 statement2; } function2 (arguments) { statement1; function 2 statement2; }
chapter 1 general user ? s manual u15556ej1v0um 24 an actual c source program looks like this. #define true 1 #define false 0 #define xxx xxx ........................ <6> preprocessing (macrodefinition) #define size 200 void printf (char *, int); xxx xxxx (xxx, xxx) ................... <7> function prototype declarator void putchar (char); char mark[size+1]; char xxx ..... <1> type declarator, <5> external definition main ( ) xx [xx] ..................................................... <2> operator { int i,prime, k, count; int xxx .......................................... <1> type declarator count = 0; xx = xx ..................................................... <2> operator for (i = 0; i <= size;i++) for (xx;xx;xx) xxx ; .................. <3> control structure mark[i] = true; for (i = 0; i <= size ; i++) { if (mark[i]) { prime = i + i + 3; xxx = xxx + xxx + xxx................ <2> operator printf (?%6d?, prime); xxx (xxx);................................... <2> operator count++; if ((count%8) = = 0) putchar ('\n'); ....................... if {xxx) xxx ; ............................ <3> control structure for (k = i + prime ; k <= size ; k += prime) mark [k] = false; } } printf (?\n%d primes found. ?, count); xxx (xxx) ; ..... <2> operator } void printf (char *s, int i) { int j; char *ss; j = i; ss = s; } void putchar (char c) char d; d = c; }
chapter 1 general user ? s manual u15556ej1v0um 25 <1> declaration of type and storage class the data type and storage class of an identifier that indicates a data object are declared. for details, see chapter 3 declaration of types and storage classes . <2> operator and expression these are the statements that instruct the compiler to perform an arithmetic operation, logical operation, assignment, etc. for details, see chapter 5 operators and expressions . <3> control structure this is a statement that specifies the program flow. c has several instructions for each of the control structures such as conditional control, iteration, and branch. for details, see chapter 6 control structures of c language . <4> structure or union a structure or union is declared. a structure is a data object that contains several subobjects or members that may have different types. a union is defined when two or more variables share the same memory. for details, see chapter 7 structures and unions . <5> external definition a function or external object is declared. a function is one element when a c language program is divided by a special-purpose or characteristic action. a c program is a collection of these functions. for details, see chapter 8 external definitions . <6> preprocessing this is an instruction for the compiler. #define instructs the compiler to replace a parameter that is the same as the first operand with the second operand if the parameter appears in the program. for details, see chapter 9 preprocessings (compiler directives) . <7> declaration of function prototype the return value and argument type of a function are declared.
chapter 1 general user ? s manual u15556ej1v0um 26 1.4 reminders before program development before commencing program development, keep in mind the points (limit values or minimum guaranteed values) summarized in table 1-1 below. table 1-1. maximum performance characteristics of this c compiler     
    
   


 
 
 

   
  
    

  
    
  !

 " 
#

#
 

$ #
  #

 
#
 #

 
  

   
%
#
!   

"& 
    

 "

'  
   !

"  
" 
" 

  


 
  (
"   ( !

"  
" 
" 

  




' #!
  )
"   * !

#! 


 
  +
#!   , !

#! 
" 
" 
! -

. " 

! - 
#!   ) !

 


 
  ++++
   + !

  

 
   

 
 )
    !

  

 
   

 
 
    !

"  

 

 +,
"    !

"  
. " 

 


-  +)
"    / 0


 
!1 ( 
!#   

 include
   ,
  ( !

case
! 

switch
  *
!  * !




   
  2'
++++
 , !



" 
 
!
  
. "
 #
     2'
++
 )  

 
  +
  + !

! 
. " 

 
! 
chapter 1 general user ? s manual u15556ej1v0um 27     
    
  3
 0


  

 -


!1
  ( 
!#   !

!



  (
!  !

enum
 

   
    


 
 



  
    

  0
  
  ( !

 
   



 
  +++ * 

"


  
 
. "
 "
 
 
   ) ,  

  ++
  ) !

4
 
 
 "
     ( notes 1. this value applies when symbols can be processed with the available memory space alone without using any temporary file. when a temporary file is used because of insufficient memory space, this value must be changed according to the file size. 2. this value includes the reserved macro definitions of the c compiler. 3. the large model provides 1,024 kb of code segments and 16 mb of data and stack segments altogether (when the ?ml option is specified). the medium model provides 1,024 kb of code segments and 64 kb of data and stack segments altogether (when the ?mm option is specified). the location ( ? cs0 or ? cs15) can be specified for both models (the default is large model, location 0fh).
chapter 1 general user ? s manual u15556ej1v0um 28 1.5 features of this c compiler this c compiler has extended functions for cpu code generation that are not supported by ansi (american national standards institute) standard c. the extended functions of the c compiler allow the special function registers for the 78k/iv series to be described at the c language level and thus help shorten object code and improve program execution speed. for details of these extended functions, see chapter 11 extended functions in this manual. outlined here are the following extended functions that help shorten object code and improve execution speed.  callt /_ _ callt functions .................. functions can be called using the callt table area.  register variables .......................... variables can be allocated to registers.  sreg /_ _ sreg /_ _ sreg1 variablesvariables can be allocated to the saddr area.  sfr area .......................................... sfr names can be used.  noauto functions............................ functions that do not output code for stack frame formation can be norec /_ _ leaf functions.................. created.  asm statements ............................. an assembly language program can be described in a c source program.  bit type variables,........................... accessing the saddr or sfr area can be made on a bit-by-bit basis. boolean /_ _ boolean type variables, _ _ boolean 1 type variables  callf /_ _ callf functions ................... a function body can be stored in the callf area.  bit field declaration ........................ a bit field can be specified with unsigned char type.  multiplication function..................... the code to multiply can be directly output with inline expansion.  division function ............................. the code to divide can be directly output with inline expansion.  rotate function ............................... the code to rotate can be directly output with inline expansion.  absolute address function.............. specific addresses in the memory space can be accessed.  data insertion function ................... specific data and instructions can be directly embedded in the code area.  _ _pascal function .......................... the used stack is corrected on the called function side.  memory manipulation function ...... memcopy and memset can be directly output with inline expansion.  callf two-step branch function ....... a two-step branch function is performed in the callf area.  three-byte address reference/generation function ....... three-byte address reference/generation is performed. an outline of the extended functions of this compiler is shown below. for details of each extended function, refer to chapter 11 . <1> callt /_ _ callt functions functions can be called by using the callt table area. the address of each function to be called (this function is called a callt function) is stored in the callt table from which it can be called later. this makes code shorter than the ordinary call instruction and helps shorten object code. <2> register variables variables declared with the register storage class specifier are allocated to the register or saddr area. instructions to the variables allocated to a register or saddr area are shorter in code length than those to memory. this helps shorten object and improves program execution speed as well.
chapter 1 general user?s manual u15556ej1v0um 29 <3> using the saddr area variables declared with the keyword sreg can be allocated to the saddr area. instructions to these sreg variables are shorter in code length than those to memory. this helps shorten object code and also improves program execution speed. variables can be allocated to the saddr area also by option (only to the saddr2 area). <4> sfr area by declaring use of sfr names, manipulations on the sfr area can be described at the c source file. <5> noauto functions functions declared as noauto do not output code for preprocessing and postprocessing (stack frame formation). by calling a noauto function, arguments are passed via registers. this helps shorten object code and improve program execution speed as well. this function has restrictions on arguments/automatic variables. for the details, refer to 11.5 (5) noauto function . <6> norec /_ _ leaf functions functions declared as norec /_ _ leaf do not output code for preprocessing and postprocessing (stack frame formation). by calling a norec /_ _ leaf function, arguments are passed via registers as much as possible. automatic variables to be used inside a norec /_ _ leaf function are allocated to register or the saddr area. this helps shorten object code and also improve program execution speed. this function has restrictions on arguments/automatic variables and is not allowed to call a function. for the details, refer to 11.5 (6) norec function . <7> bit type variables and boolean /_ _boolean type variables variables with a 1-bit storage area are generated. by using the bit type variable or boolean/_ _ boolean type variable, the saddr2 area can be accessed in bit units. the boolean /_ _boolean type variable is the same as the bit type variable in terms of both function and usage. <8> boolean1 type variables variables with a 1-bit storage area are generated. by using the _ _ boolean1 type variable, the saddr1 area can be accessed in bit units. the _ _boolean1 type variable is the same as the bit type variable in terms of both function and usage. <9> asm statements the assembler source program described by the user can be embedded in an assembler source file to be output by this c compiler. <10> interrupt functions a vector table and an object code corresponding to the interrupt are output. this allows programming of interrupt functions at the c source level. <11> interrupt function qualifier this qualifier allows the setting of a vector table and interrupt function definitions to be described in a separate file.
chapter 1 general user?s manual u15556ej1v0um 30 <12> interrupt function an interrupt disable instruction and an interrupt enable instruction are embedded in an object. <13> cpu control instructions each of the following instructions is embedded in an object. instruction to set the value for halt to the stbc register instruction to set the value for stop to the stbc register brk instruction nop instruction <14> callf /_ _ callf function the callf instruction stores the body of a function in the callf entry area and allows the calling of the function with a code shorter than that with the call instruction. this improves executing speed and shortens the object code. <15> usage of 16 mb expansion space object files that linearly access the 16 mb expansion space are generated by an option. <16> location function the location of the saddr area can be changed by an option if the memory model is large or medium. <17> absolute address access function codes that access the ordinary memory space are created with direct inline expansion without resort to a function call, and an object file is created. <18> bit field declaration by specifying a bit field to be unsigned char type, the memory can be saved, object code can be shortened, and execution speed can be improved. <19> function to change compiler output section name by changing the compiler section output name, the section can be independently allocated with a linker. <20> binary constant description function binary can be described in the c source. <21> module name change functions object module names can be freely changed in the c source. <22> rotate function the code to rotate the value of an expression to the object can be directly output with inline expansion. <23> multiplication function the code to multiply the value of an expression to the object can be directly output with inline expansion. this function can shorten the object code and improve the execution speed. <24> division function the code to divide the value of an expression to the object can be directly output with inline expansion. this function can shorten the object code and improve the execution speed.
chapter 1 general user ? s manual u15556ej1v0um 31 <25> data insertion function constant data is inserted in the current address. specific data and instructions can be embedded in the code area without using assembler description. <26> interrupt handler for rtos interrupt handlers for the rx78k/iv (real-time os) can be described. vectors can be set (settings of interrupt request name, function name for handlers, and stack switching) by the #pragma directive. <27> interrupt handler qualifier for rtos this qualifier allows the interrupt handler description and the vector setting for the rx78k/iv (real-time os) to be made in separate files. <28> task function for rtos specified functions are interpreted as the tasks for the rx78k/iv (real-time os) by the #pragma directive. this allows the description of task function for rtos with better code-efficiency at the c source level. <29> changing function call interface arguments can be passed by the previous function interface specification (using the stack only, with cc78k4 ver.1.00 compatibles) by specifying the -zo option during compilation. <30> change of calculation method of offset of arrays and pointers the code efficiency is improved by performing an unsigned index calculation for the offset of the arrays and pointers (distance from the start of the array or pointer). <31> pascal function (_ _pascal ) the stack correction used to place arguments during the function call is performed on the called function side, not on the side calling the function. this shortens the object code when there are function calls in many places. <32> automatic pascal functionization of function call interface _ _ pascal attributes are added to all functions that can be pascal functionized. <33> flash area allocation method object files to be allocated to the flash area are generated. <34> flash area branch table startup routines and interrupt functions can be allocated to the flash area. a function can be called from the boot area to the flash area. <35> function call function from boot area to flash area a function in the flash area can be called from the boot area. <36> firmware rom function manipulations regarding the firmware rom function can be described at the c source level.
chapter 1 general user ? s manual u15556ej1v0um 32 <37> limiting int expansion of argument/return value when the argument/return value of a function has the char/unsigned type, object files that do not perform int expansion are generated. this method can shorten the object code and improve the execution speed. <38> memory manipulation function memory manipulation functions can be output to an object directly with inline expansion. this function can shorten the object code and improve the execution speed. <39> callf two-step branch function compared when a function body is allocated in the callf area, the callf/_ _callf attribute can be added to many more functions. therefore, this function can shorten the object code if many functions that include call function are frequently used. <40> automatic callf functionization of function call interface the _ _ callf attribute is added to all functions except for the callt/_ _callt  _ _interrupt/_ _interrupt_brk/_ _rtos_interrupt functions. <41> three-byte address reference/generation function three-byte address reference/generation can be performed with a short code without using a complex cast description. <42> absolute address allocation specification the external variable that declared _ _directmap and a static variable in a function can be allocated to any address, and multiple variables can be allocated in duplicate to the same address.
user?s manual u15556ej1v0um 33 chapter 2 constructs of c language this chapter explains the constituent elements of a c source module file. a c source module file consists of the following tokens (distinguishable units in a sequence of characters). keywords identifiers constants string literal operators delimiters header name no. of preprocesses comment the tokens used in a c program description example are shown below. #include ?expand. h? extern void testb (void); extern ........................................................... keyword extern void chgb (void); extern bit data1; extern bit data2; data1, data2 ................................................. identifiers void main ( ) void............................................................... keyword { data1 = 1 ; 1 ................................................................... constant data2 = 0 ; 0 ................................................................... constant while(data1) { while ............................................................. keyword data1 = data2 ; { } ................................................................. delimiter testb( ) ; = ................................................................... operator } if (data1 && data2) { if.................................................................... keyword chgb ( ) ; &&................................................................. operator } ( ) ................................................................. operator } void lprintf (char *s, int i) lprintf............................................................. identifier { char, int......................................................... keywords int j; s, i................................................................. identifiers char *ss; * .................................................................... operator j = i; ss = s; } . . .
chapter 2 constructs of c language user?s manual u15556ej1v0um 34 2.1 character sets (1) character sets character sets to be used in c programs include a source character set to be used to describe a source file and an execution character set to be interpreted in the execution environment. the value of each character in the execution character set is represented by jis code. the following characters can be used in the source character set and execution character set. 26 uppercase letters abcdefghijklm nopqrstuvwxyz 26 lowercase letters abcdefghijklm nopqrstuvwxyz 10 decimal numbers 0123456789 29 graphic characters !?#%&?()*+,-./: ;<=>?[]^?{|}~ and nonprintable control characters which indicate space, horizontal tab, vertical tab, form feed, etc. remark in character constants, string literals, and comment statements, characters other than the above may also be used.
chapter 2 constructs of c language user?s manual u15556ej1v0um 35 (2) escape sequences nongraphic characters used for control characters such as alert, form feed are represented by escape sequences. each escape sequence consists of a backslash (\) and a letter. nongraphic characters represented by escape sequences are shown below. table 2-1. list of escape sequences escape sequence meaning character code \a alert 07h \b backspace 08h \f form feed 0ch \n line feed 0ah \r carriage return 0dh \t horizontal tab 09h \v vertical tab 0bh (3) trigraph sequences when a source file includes a list of the three characters (called ?trigraph sequence?) shown in the left column of the table below, the list of the three characters is converted into the corresponding single character shown in the right column. table 2-2. list of trigraph sequence trigraph sequence meaning ??= # ??( [ ??/ \ ??) ] ??? ^ ??< { ??! | ??> } ??- ~
chapter 2 constructs of c language user?s manual u15556ej1v0um 36 2.2 keywords (1) ansi keywords the following tokens are used by the c compiler as keywords and thus cannot be used as labels or variable names. auto break case char const continue default do double else enum extern for float goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while (2) keywords added for the cc78k4 in this c compiler the following tokens have been added as keywords to implement its expanded functions. as with ansi keywords, hese tokens cannot be used as labels or variable names (when an uppercase character is included, the token is not regarded as a keyword). keywords that do not start with ?_ _? can be made invalid by specifying the option that enables only ansi-c language specification ( ?za ). _ _callt/callt ................................. declaration of callt function _ _callf/callf ................................. declaration of callf function _ _sreg/sreg ...................................... declaration of sreg variable _ _sreg1 .............................................. declaration of sreg1 variable noauto ................................................... declaration of noauto function _ _leaf/norec .................................... declaration of norec function bit ......................................................... declaration of bit type variable _ _boolean/boolean ......................... declaration of boolean type variable _ _boolean1 ........................................ declaration of boolean1 type variable _ _interrupt ...................................... hardware interrupt function _ _interrupt_brk ............................. software interrupt function _ _asm ................................................... asm statement _ _rtos_interrupt ........................... interrupt handler for rtos _ _pascal ............................................ pascal function _ _flash .............................................. firmware rom function _ _directmap ...................................... absolute address allocation specification
chapter 2 constructs of c language user?s manual u15556ej1v0um 37 2.3 identifiers an identifier is the name given to the following variables. function object tag of structure, union, or enumeration type member of structure, union, or enumeration type typedef name label name macro name macro parameter each identifier can consist of uppercase letters, lowercase letters, or numeric characters including underscores. the following characters can be used as identifiers. there is no restriction on the maximum length of the identifier. in this compiler, however, only the first 249 characters can be identified (refer to table 1-1 maximum performance characteristics of this c compiler ). _ (underscore) a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 all identifiers must begin with other than a numerical character (namely, a letter or an underscore) and must not be the same as any keyword. 2.3.1 scope of identifiers the range of an identifier within which its use becomes effective is determined by the location at which the identifier is declared. the scope of identifiers is divided into the following four types. function scope file scope block scope function prototype scope
chapter 2 constructs of c language user?s manual u15556ej1v0um 38 extern _ _ boolean data1, data2; data1, data2.......................................................... file scope void testb(int x); x...................................................... function prtotype scope void main(void) { int cot ; cot...................................................................... block scope data1 = 1 ; data2 = 0 ; while(data1) { data1 = data2; j1 : j1................................................................... function scope testb (cot) ; } } void testb(int x) x......................................................................... block scope { . . . (1) function scope function scope refers to the entirety within a function. an identifier with function scope can be referenced from anywhere within a specified function. identifiers that have function scope are label names only. (2) file scope file scope refers to the entirety of a translation (compiling) unit. identifiers that are declared outside a block or parameter list all have file scope. an identifier that has file scope can be referenced from anywhere within the program. (3) block scope block scope refers to the range of a block (a sequence of declarations and statements enclosed by a pair of curly braces { } which begins with the opening brace and ends with the closing brace). identifiers that are declared inside a block or parameter list all have block scope. an identifier that has block scope is effective until the innermost brace pair including the declaration of the identifier is closed. (4) function prototype scope function prototype scope refers to the range of a declared function from beginning to end. identifiers that are declared inside a parameter list within a function prototype all have function prototype scope. an identifier that has function prototype scope is effective within a specified function.
chapter 2 constructs of c language user?s manual u15556ej1v0um 39 2.3.2 linkage of identifiers the linkage of an identifier refers to the case when the same identifier declared more than once in different scopes or in the same scope can be referenced as the same object or function. by being linked an identifier is regarded to be one and the same. an identifier may be linked in the following three different ways: external linkage, internal linkage and no linkage. (1) external linkage external linkage refers to identifiers to be linked in translation (compiling) units that constitute the entire program and as a collection of libraries. the following identifiers are examples of external linkage.  the identifier of a function declared without a storage class specification.  the identifier of an object or function declared as extern , which has no storage class specification  the identifier of an object which has file scope but has no storage class specification (2) internal linkage internal linkage refers to identifiers to be linked within one translation (compiling) unit. the following identifier is an example of internal linkage.  the identifier of an object or function that has file scope and contains the storage class specifier static . (3) no linkage an identifier that has no linkage to any other identifier is an inherent entity. examples of identifiers that have no linkage are as follows.  an identifier that does not refer to a data object or function  an identifier declared as a function parameter  the identifier of an object that does not have the storage class specifier extern inside a block 2.3.3 name space for identifiers all identifiers are classified into the following ?name spaces?.  label name...................................................... distinguished by a label declaration.  tag name of structure, union, or enumeration distinguished by the keyword struct, union or enum  member name of structure or union ................ distinguished by the dot (.) operator or arrow ( ) operator.  ordinary identifiers (other than above)............ declared as ordinary declarators or enumeration type constants. 2.3.4 storage duration of objects each object has a storage duration that determines its lifetime (how long it can remain in memory). this storage duration is divided into the following two categories: static storage duration and automatic storage duration. (1) static storage duration before executing an object program that has a static duration, an area is reserved for objects and values to be stored are initialized once. the objects exist throughout the execution of the entire program and retain the values last stored. objects that have a static storage duration are as shown below.  objects that have external linkage  objects that have internal linkage  objects declared by the storage class specifier static
chapter 2 constructs of c language user?s manual u15556ej1v0um 40 (2) automatic storage duration for objects that have automatic storage duration, an area is reserved when they enter a block to be declared. if initialization is specified, the objects are initialized as they enter from the beginning of the block. in this case, if any object enters the block by jumping to a label within the block, the object will not be initialized. for objects that have automatic storage duration, the reserved area will not be guaranteed after the execution of the declared block. objects that have automatic storage duration are as follows.  objects that have no linkage  objects declared inside a block without the storage class specifier static 2.3.5 data types a type determines the meaning of the value to be stored in each object. data types are divided into the following three categories depending on the variable to be declared.  object type ................................................... type that indicates an object with size information  function type ................................................ type that indicates a function  incomplete type ............................................ type that indicates an object without size information  basic types integral types char type (arithmetic types) signed signed char integral short int types int long int unsigned integral types (specified by unsigned ) enumeration type floating point types float double long double  character types char signed char unsigned char  incomplete types array with an indefinite object size, structure, union, and void type  derived types array type structure type union type function type pointer type  scalar types basic (arithmetic types) pointer type aggregate type
chapter 2 constructs of c language user?s manual u15556ej1v0um 41 (1) basic types a collection of basic data types is also referred to as ?arithmetic types?. the arithmetic types consist of integral types and floating-point types. (a) integral types integral data types are subdivided into four types. each of these types has a value represented by the binary numbers 0 and 1.  char type  signed integral type  unsigned integral type  enumeration type (i) char type the char the type has a sufficient size to store any character in the basic execution character set. the value of the character to be stored in a char type object becomes positive. data other than characters is handled as an unsigned integer. in this case, however, if an overflow occurs, the overflowed part will be ignored. (ii) signed integral type the signed integral type is subdivided into the following four types.  signed char  short int  int  long int an object declared with the signed char type has an area of the same size as the char type without a qualifier. an int object without a qualifier has a size natural to the cpu architecture of the execution environment. a signed integral type data has its corresponding unsigned integral type data. both share an area of the same size. the positive number of a signed integral type data is a partial collection of unsigned integral type data. (iii) unsigned integral data the unsigned integral type is a data defined with the unsigned keyword. no overflow occurs in any computation involving unsigned integral type data. this is because if the result of a computation involving unsigned integral type data becomes a value which cannot be represented by an integral type, the value will be divided by the maximum number which can be represented by an unsigned integral type plus 1 and substituted with the remainder in the result of the division. (iv) enumeration type enumeration is a collection or list of named integer constants. an enumeration type consists of one or more sets of enumeration.
chapter 2 constructs of c language user?s manual u15556ej1v0um 42 (b) floating-point types the floating-point types are subdivided into three types. ?float  double  long double in this compiler, double and long double types as well as the float type are supported as a floating-point expression for the single precision normalized number that is specified in ansi/ieee 754-1985 . thus, float , double , and long double types have the same value range. table 2-3. list of basic data types type value range ( signed ) char ?128 to +127 unsigned char 0 to 255 ( signed ) short int ?32768 to +32767 unsigned short int 0 to 65535 ( signed ) int ?32768 to +32767 unsigned int 0 to 65535 ( signed ) long int ?2147483648 to +2147483647 unsigned long int 0 to 4294967295 float 1.17549435e?38f to 3.40282347e+38f double 1.17549435e?38f to 3.40282347e+38f long double 1.17549435e?38f to 3.40282347e+38f  the signed keyword may be omitted. however, with the char type, it is judged as signed char or unsigned char depending on the condition at compilation.  short int data and int data are handled as data that have the same value range but are of different types.  unsigned short int data and unsigned int data are handled as data that have the same value range but are of different types.  float , double , and long double data are handled as data that have the same value range but are of different types.
chapter 2 constructs of c language user?s manual u15556ej1v0um 43 (i) floating-point number (float type) specifications format the floating-point number format is shown below. (higher address) s e m (lower address) 31 30 23 22 0 the numerical values in this format are as follows. (value of sign) (value of exponent) (?1) * (value of mantissa) *2 s: sign (1 bit) 0 for a positive number and 1 for a negative number. e: exponent (8 bits) an exponent with a base of 2 is expressed as a 1-byte integer (expressed by two?s complement in the case of a negative), and used after having a further bias of 7fh added. these relationships are shown in table 2-4 below. table 2-4. exponent relationships exponent (hexadecimal) value of exponent fe 127 ? ? ? ? ? ? 81 2 80 1 7f 0 7e ?1 ? ? ? ? ? ? 01 ?126 m: mantissa (23 bits) the mantissa is expressed as an absolute value, with bit positions 22 to 0 equivalent to the 1st to 23rd places of a binary number. except for when the value of the floating point is 0, the value of the exponent is always adjusted so that the mantissa is within the range of 1 to 2 (normalization). the result is that the position of 1 (i.e. the value of 1) is always 1, and is thus represented by omission in this format.  zero expression when exponent = 0 and mantissa = 0, 0 is expressed as follows. (value of sign) (?1) * 0
chapter 2 constructs of c language user?s manual u15556ej1v0um 44  infinity expression when exponent = ffh and mantissa = 0, is expressed as follows. (value of sign) (?1) *  unnormalized value when exponent = 0 and mantissa 0, the unnormalized value is expressed as follows. (value of sign) ?126 (?1) * (value of mantissa) *2 remark the mantissa value here is a number less than 1, so bit positions 22 to 0 of the mantissa express as is the 1st to 23rd decimal places.  not-a-number (nan) expression when exponent = ffh and mantissa 0, nan is expressed, regardless of the sign.  operation result rounding numerical values are rounded down to the nearest even number. if the operation result cannot be expressed in the above floating-point format, round to the nearest expressible number. if there are two values that can express the differential of the prerounded value, round to an even number (a number whose lowest binary bit is 0).  operation exceptions there are five types of operation exceptions, as shown below. table 2-5. list of operation exceptions exception return value underflow unnormalized number inexact 0 overflow zero division operation impossible not-a-number (nan) calling the matherr function causes a warning to appear when an exception occurs. (2) character types the character data types include the following three types.  char  signed char  unsigned char
chapter 2 constructs of c language user?s manual u15556ej1v0um 45 (3) incomplete types the incomplete data types include the following four types.  arrays with indefinite object size  structures  unions  void type (4) derived types the derived types are divided into the following three categories.  array type  structure type  union type  function type  pointer type (a) aggregate type the aggregate type is subdivided into two types. array type and structure type. an aggregate type data is a collection of member objects to be taken successively. i) array type the array type continuously allocates a collection of member objects called element types. member objects all have an area of the same size. the array type specifies the number of element types and the elements of the array. it cannot create an incomplete type array. ii) structure type the structure type continuously allocates member objects each differing in size. each member object can be specified by name. (b) union type the union type is a collection of member objects that overlap each other in memory. these member objects differ in size and name and can be specified individually. (c) function type the function type represents a function that has a specified return value. function type data specifies the type of return value, the number of parameters, and the type of parameter. if the type of return value is t, the function is referred to as a function that returns t. (d) pointer type the pointer type is created from a function type object type called a referenced type as well as from an incomplete type. the pointer type represents an object. the value indicated by the object is used to reference the entity of a referenced type. a pointer type data created from the referenced type t is called a pointer to t. (5) scalar types the basic types (arithmetic types) and pointer type are collectively called the scalar types. the scalar types include the following data types.  char type  signed integral type  unsigned integral type  enumeration type  floating point type  pointer type
chapter 2 constructs of c language user?s manual u15556ej1v0um 46 2.3.6 compatible type and composite type (1) compatible type if two types are the same, they are said to be compatible or have compatibility. for example, if two structures, unions, or enumeration types that are declared in separate translation (compiling) units have the same number of members, the same member name and compatible member types, they have a compatible type. in this case, the individual members of the two structures or unions must be in the same order and the individual members (enumerated constants) of the two enumerated types must have the same values. all declarations related to the same objects or functions must have a compatible type. (2) composite type a composite type is created from two compatible types. the following rules apply to the composite type.  if either of the two types is an array of known type size, the composite type is an array of that size.  if only one of the types is a function type which has a parameter type list (declared with a prototype), the composite type is a function prototype that has the parameter type list.  if both types have a parameter type list (i.e., functions with prototypes), the composite type is one with a prototype consisting of all information that can be combined from the two prototypes. [example of composite type] assume that two declarations that have file scope are as follows. int f (int (*) (), double (*) [3] ) ; int f (int (*) (char *), double (*) [] ); the composite type of the function in this case becomes as follows. int f (int (*) (char *), double (*) [3] ) ; 2.4 constants a constant is a variable that does not change in value during the execution of the program, and its value must be set beforehand. the type for each constant is determined according to the format and value specified for the constant. the following four constant types are available.  floating-point constants  integer constants  enumeration constants  character constants
chapter 2 constructs of c language user?s manual u15556ej1v0um 47 2.4.1 floating-point constant a floating-point constant consists of an effective digit part, exponent part, and floating-point suffix. effective digit part: integer part, decimal point, and fraction part exponent part: e or e, signed exponent floating-point suffix: f/f ( float ) i/l ( long double ) if omitted ( double ) the signed exponent of the exponent part and the floating-point suffix can be omitted. either the integer part or fraction part must be included in the effective digits. also, either the decimal point or exponent part must be included (example: 1.23f, 2e3). 2.4.2 integer constant an integer constant starts with a number and does not have a decimal point or exponent part. an unsigned suffix can be added after the integer constant to indicate that the integer constant is unsigned. a long suffix can be added after the integer constant to indicate that the integer constant is long. there are the following three types of integer constants.  decimal constant: decimal number that starts with a number other than 0 decimal number = 123456789  octal constant: integer suffix 0 + octal number octal number = 01234567  hexadecimal constant: integer suffix 0x or 0x + hexadecimal number hexadecimal number = 0123456789 abcdef abcdef unsigned suffix u u long suffix l l (1) decimal constant a decimal constant is an integer value with a base (radix) of 10 and must begin with a number other than 0 followed by any numbers 0 through 9 (example: 56ul). (2) octal constant an octal constant is an integer value with a base of 8 and must begin with 0 followed by any numbers 0 through 7 (example: 034u). (3) hexadecimal constant a hexadecimal constant is an integer value with the base of 16 and must begin with 0x or 0x followed by any numbers 0 through 9 and a through f or a through f, which represent 10 through 15 (example: 0xf3). the type of integer constant is regarded as the first of the ?representable type? shown below. in this compiler, the type of the unsubscripted constant can be changed to char or unsigned char depending on the compile condition (option).
chapter 2 constructs of c language user?s manual u15556ej1v0um 48 (integer constant) (representable type)  unsuffixed decimal number .................................. int , long int , unsigned long int  unsuffixed octal, hexadecimal number................. int , unsigned int , long int , unsigned long int  suffixed u or u...................................................... unsigned int , unsigned long int  suffixed l or l........................................................ long int , unsigned long int  suffixed u or u, and suffixed l or l ....................... unsigned long int 2.4.3 enumeration constants enumeration constants are used for indicating an element of an enumeration type variable, that is, the value of an enumeration type variable that can have only a specific value indicated by an identifier. the enumeration type (enum) is whichever is the first type from the top of the list of three types shown below that can represent all the enumeration constants. the enumeration constant is indicated by the identifier.  signed char  unsigned char  signed int it is described as ? enum enumeration type {list of enumeration constant}?. example enum months {january = 1, february, march, april, may}; when the integer is specified with =, the enumeration variable has the integer value, and the following value of enumeration variable has that integer value + 1. in the example shown above, the enumeration variable has 1, 2, 3, 4, 5, respectively. when there is not ?= 1?, each constant has 0, 1, 2, 3, 4, 5, respectively. 2.4.4 character constants a character constant is a character string that includes one or more characters enclosed in a pair of single quotes as in ?x? or ?ab?. a character constant does not include single quote?, backslash ( or \), and line feed character (n). to represent these characters, escape sequences are used. there are the following three types of escape sequences.  simple escape sequence: \? \? \? \ \a \b \f \n \r \t \v  octal escape sequence: \octal number [octal number octal number] (example: \012, \0 note 1 )  hexadecimal escape sequence : \x hexadecimal number (example: \xff note 2 ) notes 1. null character 2. in this compiler, \xff represents ?1. if the condition (option) that regards char as unsigned char is added, however, it represents +255.
chapter 2 constructs of c language user?s manual u15556ej1v0um 49 2.5 string literals a string literal is a string of zero or more characters enclosed in a pair of double quotes as in ?xxx?. (example: ?xyz?) a single quote (?) is represented by the single quotation mark itself or by the escape sequence \?, whereas a double quote (?) is represented by the escape sequence \?. array elements have a char type string literal and are initialized by assigned tokens (example: char array [ ] = ?abc?;). 2.6 operators the operators are shown below. [ ] ( ) . -> ++- -&*+?~! sizeof /%<<>><><=>===!= ^|&&|| ?: = *=/=%=+=-=<<=>>= &= ^= |= ,### the [ ], ( ), and ?: operators must always be used in pairs. an expression may be described in brackets ?[ ]?, in parentheses ?( )?, or between ??? and ?:?. the # and ## operators are used only for defining macros in preprocessings. (for the description, refer to chapter 5 operators and expressions .) 2.7 delimiters a delimiter is a symbol that has an independent syntax or meaning. however, it never generates a value. the following delimiters are available for use in c. [ ] ( ) { } * , : = ; ... # an expression declaration or statement may be described in brackets ?[ ]?, parentheses ?( )?, or braces ?{ }?, these delimiters must always be used in pairs as shown above. the delimiter # is used only for preprocessings.
chapter 2 constructs of c language user?s manual u15556ej1v0um 50 2.8 header name a header name indicates the name of an external source file. this name is used only in the preprocessing directive ?#include ?. an example of the #include directive header name is shown below. for details of each #include directive, refer to 9.2 source file inclusion directive . #include
#include ?header name? 2.9 comment a comment refers to a statement to be included in a c source module for information only. it begins with ?/*? and ends with ?*/?. the part after ?//? to the line feed can be identified as a comment statement using the ?zp option. example /* comment statement */ //comment statement
user?s manual u15556ej1v0um 51 chapter 3 declaration of types and storage classes this chapter explains how data (variables) or functions to be used in c should be declared as well as the scope for each data or function. a declaration means the specification of an interpretation or attribute for an identifier or a collection of identifiers. a declaration to reserve a storage area for an object or function named by an identifier is referred to as a ?definition?. an example of a declaration is shown below. #define true 1 #define false 0 #define size 200 void main(void) { auto int i, prime, k; /* declaration of automatic variables */ for ( i = 0 ; i <= size ; i++) mark [i] = true ; . . . a declaration is configured with a storage class specifier, type specifier, initialize declarator, etc. the storage class specifier and type specifier specify the linkage, storage duration, and the type of entity indicated by the declarator. an initialize declarator list is a list of declarators delimited with a comma. each declarator may have additional type information or initializer or both. if an identifier for an object is declared to have no linkage, the type of the object must be perfect (the object with information related to the size) at the end of the declarator or initialize declarator (if it has an initializer).
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 52 3.1 storage class specifiers a storage class specifier specifies the storage class of an object. it indicates the storage location of a value that the object has, and the scope of the object. in a declaration, only one storage class specifier can be described. the following five storage class specifiers are available. ? typedef  extern  static  auto  register (1) typedef the typedef specifier declares a synonym for the specified type. see 3.6 below for details of the typedef specifier. (2) extern the extern specifier indicates (tells the compiler) that the variable immediately before this specifier is declared elsewhere in the program (i.e., an external variable). (3) static the static specifier indicates that an object has static storage duration. for an object that has static storage duration, an area is reserved before the program execution and the value to be stored is initialized only once. the object exists throughout the execution of the entire program and retains the value last stored in it. (4) auto the auto specifier indicates that an object has automatic storage duration. for an object that has automatic storage duration, an area is reserved when the object enters a block to be declared. at entry into the declared block from its top, the object is initialized if so specified. if the object enters the block by jumping to a label within the block, the object will not be initialized. the area reserved for an object that has automatic storage duration will not be guaranteed after the execution of the declared block. (5) register the register specifier indicates that an object is assigned to a register of the cpu. with this c compiler, it is allocated to the register or saddr area of the cpu. see chapter 11 extended functions for details of register variables.
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 53 3.2 type specifiers a type specifier specifies (or refers to) the type of an object. the following type specifiers are available.  void  char  short  int  long  float  double  long double  signed  unsigned  structure or union specifier  enumeration specifier  typedef name in this c compiler, the following type specifiers have been added.  bit/boolean/_ _boolean/_ _boolean1
chapter 3 declaration of t ypes and storage cla sses user?s manual u15556ej1v0um 54 the following explains the meaning of each type specifier and the limit values that can be expressed with this compiler (the values enclosed in the parentheses). since this compiler supports only the single precision of ieee std 754-1985 for floating-point operations, double and long double data are regarded to have the same format as those of float data. ? void ............................................................... collection of null values  char ............................................................... size of the basic character set that can be stored  signed char ................................................ signed integer (?128 to +127)  unsigned char ............................................ unsigned integer (0 to 255)  short, signed short, short int, signed short int ........................................ signed integer (?32768 to +32767)  unsigned short, unsigned short int unsigned integer (0 to 65535)  int, signed, signed int ...................... signed integer (?32768 to +32767)  unsigned, unsigned int ........................ unsigned integer (0 to 65535)  long, signed long, long int, signed long int ........................................ signed integer (?2147483648 to +2147483647)  unsigned long, unsigned long int ... unsigned integer (0 to 4294967295)  float ............................................................. single precision floating-point number (1.17549435e?38f to 3.40282347e+38f)  double ........................................................... double precision floating-point number (1.17549435e?38f to 3.40282347e+38f)  long double ................................................ extended precision floating-point number (1.17549435e?38f to 3.40282347e+38f)  structure/union specifier........................ collection of member objects  enumeration specifier ................................ collection of int type constants  typedef name ............................................. synonym of specified type  bit/boolean/ _ _ boolean/ _ _ boolean1 integers represented with a single bit (0 to 1) type specifiers delimited with a comma have the same size.
chapter 3 declaration of t ypes and storage cla sses user?s manual u15556ej1v0um 55 3.2.1 structure specifier and union specifier both the structure specifier and union specifier indicate a collection of named members (objects). these member objects can have different types from one another. (1) structure specifier the structure specifier declares a collection of two or more different types of variables as one object. each type of object is called a member and can be given a name. for members, continuous areas are reserved in the order of their declaration. align data is inserted by specifying the -rp option. a structure is declared as follows. the declaration will not yet allocate memory since it does not have a list of structure variables. for the definition of the structure variables, refer to chapter 7 structures and unions . struct identifier {member declaration list} ; example of structure declaration struct tnode { int count; struct tnode *left, *right; }; (2) union specifier the union specifier declares a collection of two or more different types of variables as one object. each type of object is called a member and can be given a name. the members of a union overlap each other in area, namely, they share the same area. a union is declared as follows. the declaration will not yet allocate memory since it does not have a list of union variables. for the definition of the union variables, refer to chapter 7 structures and unions . union identifier {member declaration list} ; example of union declaration union u_tag { int var1 ; long var2 ; }; each member object can be any type other than the incomplete types or function types. a member can be declared with the number of bits specified. a member with the number of bits specified is called a bit field. in this compiler, extended functions related to bit field declaration have been added. for details, refer to 11.5 (19) bit field declaration .
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 56 (3) bit field a bit field is an integral type area consisting of a specified number of bits. for the bit field, int type, unsigned int type, and signed int type data can be specified. note 1 the msb of an int field which has no qualifier or a signed int field will be judged as a sign bit. note 2 if two or more bit fields exist, the second and subsequent bit fields are packed into the adjacent bit positions, provided there is an ample space within the same memory unit. by placing an unnamed bit field with a width of 0, the next bit field will not be packed into a space within the same memory unit. an unnamed bit field has no declarator and declares a colon and a width only. the unary & operator (address) cannot be applied to a bit field object. notes 1. in this compiler, char type, unsigned char type, and signed char type can also be specified. all of them are regarded as unsigned type since this compiler does not support signed type bit fields. 2. in this compiler, the direction of bit field allocation can be changed using the compiler option ?rb (for details, refer to chapter 11 extended functions) . the following shows an example of a bit field. struct data { unsigned int a:2; unsigned int b:3; unsigned int c:1; } no1 ; 3.2.2 enumeration specifiers an enumeration type specifier indicates a list of objects to be put in sequence. objects to be declared with the enum specifier will be declared as constants that have int types. the enumeration specifier is declared as shown below. enum [identifier] {enumerator list} objects are declared according to an enumerator list. values are defined for all objects in the list in the order of their declaration by assigning the value of 0 to the first object and the value of the previous object plus 1 to the 2nd and subsequent objects. a constant value may also be specified by ? = ? . in the following example, ? hue ? is assumed as the tag name of the enumeration, ? col ? as an object that has this (enum) type, and ? cp ? as a pointer to an object of this type. in this declaration, the values of the enumeration become ? {0, 1, 20, 21} ? .
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 57 enum hue { chartreuse, burgundy, claret=20, winedark } ; enum hue col, *cp ; void main (void) { col = claret ; cp = &col ; /*...*/ (*cp != burgundy) /*...*/ : 3.2.3 tags a tag is a name given to a structure, union, or enumeration type. a tag has a declared data type and objects of the same type can be declared with a tag. the identifier in the following declaration is a tag name. structure/union identifier {member declaration list} or enum identifier {enumerator list} a tag has the contents of the structure/union or enumeration defined by a member. in the next and subsequent declarations, the structure of a struct , union , or enum type becomes the same as that of the tag ? s list. in the subsequent declarations within the same scope, the list enclosed in braces must be omitted. the following type specifier is undefined with respect to its contents and thus the structure or union has an incomplete type. struct/union identifier a tag to specify the type of this type specifier can be used only when the object size is unnecessary. this is because by defining the contents of the tag within the same scope, the type specification becomes incomplete. in the following example, the tag ? tnode ? specifies a structure that includes pointers to an integer and two objects of the same type. struct tnode { int count; struct tnode *left, *right ; }; the next example declares ? s ? as an object of the type indicated by the tag (tnode) and ? sp ? as a pointer to the object of the type indicated by the tag. by this declaration, the expression ? sp left ? indicates a pointer to ? struct tnode ? on the left of the object pointed to by ? sp ? and the expression ? s.right count ? indicates ? count ? , which is a member of ? struct tnode ? on the right of ? s ? .
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 58 typedef struct tnode tnode; struct tnode { int count ; struct tnode *left, *right ; }; tnode s *sp; void main (void) { sp left = sp right; s.right count = 2; } 3.3 type qualifiers two type qualifiers are available: const and volatile . these type qualifiers affect left-side values only. using a left-side value that has a non- const type qualifier cannot change an object that has been defined with a const type qualifier. using a left-side value that has a non- volatile type qualifier cannot reference an object that has been defined with a volatile type qualifier. an object that has a volatile qualifier type can be changed by a method not recognizable by the compiler or may have other unnoticeable side effects. therefore, an expression that references this object must be strictly evaluated according to the sequence rules that regulate abstractly how programs written in c should be executed. in addition, the values to be stored last in the object at every sequence point must be in agreement with those determined by the program, except for the changes due to factors unrecognizable by the compiler as mentioned above. if an array type is specified with type qualifiers, the qualifiers apply to the array members, not the array itself. no type qualifier can be included in the specification of a function type. however, callt , _ _ callt , callf , _ _ callf , noauto , norec , _ _ leaf , _ _ interrupt , _ _ interrupt_brk , _ _rtos_interrupt, _ _pascal , which are the type qualifiers unique to this compiler mentioned in 2.1 keywords , can be included as type qualifiers. sreg , _ _sreg , _ _sreg1 , and _ _directmap are also type qualifiers. in the following example, ? real_time_clock ? can be changed by hardware, but operations such as assignment, increment, and decrement are not possible. extern const volatile int real_time_clock; an example of modifying aggregate type data with type qualifiers is shown below. const struct s { int mem;} cs = { 1 }; struct s ncs; /* object ncs is changeable */ typedef int a [2][3]; const a a = { {4, 5, 6}, {7, 8, 9} }; /* array of const int array */ int *pi; const int *pci; ncs = cs; /* correct */ cs = ncs; /* violates restriction of left-side value which has modifiable assignment operator */ pi = &ncs. mem; /* correct */ pi = &cs. mem; /* violates restriction of the type of assignment operator = */ pci = &cs. mem; /* correct */ pi = a[0]; /* incorrect:a[0] has ? const int * ? type */
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 59 3.4 declarators a declarator declares an identifier. here, pointer declarators, array declarators, and function declarators are mainly discussed. the scope of an identifier and a function or object that has a storage duration and a type are determined by a declarator. a description of each declarator is given below. 3.4.1 pointer declarators a pointer declarator indicates that an identifier to be declared is a pointer. a pointer points to (indicates) the location where a value is stored. pointer declaration is performed as follows. * type qualifier list identifier by this declaration, the identifier becomes a pointer to t1. the following two declarations indicate a variable pointer to a constant value and an invariable pointer to a variable value, respectively. const int *ptr_to_constant; int *const constant_ptr; the first declaration indicates that the value of the constant ? const int ? pointed by the pointer ? ptr_to_constant ? cannot be changed, but the pointer ? ptr_to_constant ? itself may be changed to point to another ? const int ? . likewise, the second declaration indicates that the value of the variable ? int ? pointed by the pointer ? constant_ptr ? may be changed, but the pointer ? constant_ptr ? itself must always point to the same position. the declaration of the invariable pointer ? constant_ptr ? can be made distinct by including a definition for the pointer type to the int type data. the following example declares ? constant_ptr ? as an object that has a const qualifier pointer type to int . typedef int *int_ptr; const int_ptr constant_ptr; 3.4.2 array declarators an array declarator declares to the compiler that an identifier to be declared is an object that has an array type. array declaration is performed as shown below. type identifier [constant expression] by this declaration, the identifier becomes an array that has the declared type. the value of the constant expression becomes the number of elements in the array. the constant expression must be an integer constant expression which has a value greater than 0. in the declaration of an array, if a constant expression is not specified, the array becomes an incomplete type. in the following example, a char type array ? a[ ] ? , which consists of 11 elements and a char type pointer array ? ap[ ] ? , which consists of 17 elements, have been declared. char a[11], *ap[17];
chapter 3 declaration of t ypes and storage cla sses user?s manual u15556ej1v0um 60 in the following two examples of declarations, ?x? in the first declaration specifies a pointer to an int type data and ?y? in the second declaration specifies an array to an int type data which has no size specification and is to be declared elsewhere in the program. extern int *x; extern int y [ ]; 3.4.3 function declarators (including prototype declarations) a function declarator declares the type of return value, argument, and the type of the argument value of a function to be referenced. function declaration is performed as follows. type identifier (parameter list or identifier list) by this declaration, the identifier becomes a function that has the parameter specified by the parameter type list and returns the value of the type declared before the identifier. parameters of a function are specified by a parameter identifier list. by these lists, an identifier, which indicates the argument and its type, are specified. a macro defined in the header file ?stdarg.h? converts the list described by the ellipsis (, ...) into parameters. for a function that has no parameter specification, the parameter list will become ?void ?. 3.5 type names a type name is the name of a data type that indicates the size of a function or object. syntax-wise, it is a function or object declaration less identifiers. examples of type names are given below.  int ............................. specifies an int type.  int * ........................... specifies a pointer to an int type.  int *[3] ....................... specifies an array that has three pointers to an int type.  int (*) [3].................... specifies a pointer to an array that has three int types.  int *( )....................... specifies a function that returns a pointer to an int type that has no parameter specification.  int (*) (void)............... specifies a pointer to a function that returns an int type that no parameter specification.  int (*const [ ]) (unsigned int, ...) .... specifies an i ndefinite number of arrays that have one parameter of unsigned int type and an invariable pointer to each function that returns an int type. 3.6 typedef declarations the typedef keyword defines that an identifier is a synonym to a specified type. the defined identifier becomes a typedef name. the syntax of typedef names is shown below. typedef type identifier;
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 61 in the following example, ? distance ? is an int type, the type of ? metricp ? is a pointer to a function that returns an int type that has no parameter specification, the type of ? z ? is a specified structure, and ? zp ? is a pointer to this structure. typedef int miles, klicksp (); typedef struct {long re, im} complex; /*...*/ miles distance; extern klicksp *metricp; complex z, *zp; in the following example, the typedef name t is declared with a signed int type, and the typedef name plain is declared with an int type, and a structure with three bit field members is declared. the bit field members are as follows.  bit field member with name t and the value 0 to 15  bit field member without a name and the const qualified value ? 16 to +15 (if accessed)  bit field member with name r and the value ? 16 to +15 typedef signed int t; typedef int plain; struct tag { unsigned t:4; const t:5; plain r:5; }; in this example, these two bit field declarations differ in that the first bit field declaration has unsigned as the type specifier (therefore, t becomes the name of the structure member), and the second bit field declaration, on the other hand, has const as the type qualifier (qualifies t which can be referred to as the typedef name). after this declaration, if: t f(t (t)); long t; is found within the effective range, the function f is declared as ? function that has one parameter and returns signed int ? , and the parameter is declared as ? pointer type for the function that has one parameter and returns signed int ? . the identifier t is declared as long type. typedef names may be used to facilitate program reading. for example, the following three declarations for the function signal all specify the same type as the first declaration which does not use typedef . typedef void fv(int) ; typedef void (*pfv) (int) ; void (*signal (int, void (*) (int)))(int); fv *signal(int, fv *); pfv signal(int, pfv);
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 62 3.7 initialization initialization refers to setting a value in an object beforehand. an initializer carries out the initialization of an object. initialization is performed as follows. object = {initializer list} an initializer list must contain initializers for the number of objects to be initialized. all expressions in initializers or an initializer list for objects that have static storage duration and objects that have an aggregate type or a union type must be specified with constant expressions. identifiers that declare block scope but have external or internal linkage cannot be initialized. (1) initialization of objects which have a static storage duration if no attempt is made to initialize an arithmetic type object that has static storage duration, the value of the object will be implicitly initialized to 0. likewise, a pointer type object that has a static storage duration will be initialized to a null pointer constant. example unsigned int gval1; /* initialized by 0 */ static int gval2; /* initialized by 0 */ void func (void) { static char aval; /* initialized by 0 */ } (2) initialization of objects that have an automatic storage duration the value of an object that has automatic storage duration becomes undefined and will not be guaranteed if it is not initialized. example void func(void){ char aval; /*undefined at this point */ : aval = 1; /* initialized to 1 */ } (3) initialization of character arrays a character array can be initialized by a char string literal ( char string enclosed with ? ? ). likewise, a character string in which a series of char string literals are contained initializes the individual members or elements of an array. in the following example, the array objects ? s ? and ? t ? with no type qualifier are defined and the elements of each array will be initialized by a char string literal. char s[ ] = ?abc?, t[3] = ?abc? ;
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 63 the next example is the same as the above example of array initialization. char s[ ] = {?a?, ?b?, ?c?, ?\0?}, t[ ] = { ?a?, ?b?, ?c?}; the next example defines p as ? pointer to char ? type and the member is initialized by a character string literal so that the length indicates 4 ? char array ? type objects. char *p = ?abc? ; (4) initialization of aggregate or union type objects  aggregate type an aggregate type object is initialized by a list of initializers described in ascending order of subscripts or members. the initializer list to be specified must be enclosed in braces. if the number of initializers in the list is less than the number of aggregate members, the members not covered by the initializers will be implicitly initialized just the same as an object that has static storage duration. with an array of an unknown size, the number of its elements is governed by the number of initializers and the array will no longer become an incomplete type.  union type a union type object is initialized by an initializer for the first member of the union that is enclosed in braces. in the following example, the array ? x ? with an unknown size will change to a one-dimensional array that has three elements as a result of its initialization. int x[ ] = {1, 3, 5} ; the next example shows a complete definition which has initializers enclosed in braces. ? {1, 3, 5} ? initializes ? y [0] [0] ? , ? y [0] [1] ? , and ? y [0] [2] ? in the 1st line of the array object ? y[0] ? . likewise, in the second line, the elements of the array objects ? y [1] ? and ? y [2] ? are initialized. the initial value of ? y[3] ? is 0 since it is not specified. char y [4] [3] = { {1, 3, 5}, {2, 4, 6}, {3, 5, 7}, }; the next example produces the same result as the above example. char z[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };
chapter 3 declaration of t ypes and storage cla sses user ? s manual u15556ej1v0um 64 in the following example, the elements in the first row of ? z ? are initialized to the specified values and the rest of the elements are initialized to 0. char z[4] [3] = { {1}, {2}, {3}, {4} }; in the next example, a three-dimensional array is initialized. q[0] [0] [0] are initialized to 1, q[1] [0] [0] to 2, and q[1] [0] [1] to 3. 4, 5 and 6 initialize q[2] [0] [0], q[2] [0] [1], and q[2] [1] [0], respectively. the rest of the elements are all initialized to 0. short q[4] [3] [2] = { {1], {2, 3} {4, 5, 6} }; the following example produces the same result as the above initialization of the three-dimensional array. short q[4][3][2] = { 1, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 4, 5, 6 }; the following example shows a complete definition of the above initialization using braces. short q [4][3][2] = { { {1}, }, { {2, 3}, }, { {4, 5, 6}, } };
user?s manual u15556ej1v0um 65 chapter 4 type conversions in an expression, if two operands differ in data type, the compiler automatically performs a type conversion operation. this conversion is similar to a change obtained by the cast operator. this automatic type conversion is called an implicit type conversion. in this chapter, this implicit type conversion is explained. type conversion operations include usual arithmetic conversions, conversions involving truncation/round off, and conversions involving sign change. table 4-1 gives a list of conversions between types.
chapter 4 t ype conversions user ? s manual u15556ej1v0um 66 table 4-1. list of conversions between types after conversion before conversion (signed) char unsigned char (signed) short int unsigned short int (signed) int unsigned int (signed) long int unsigned long int float double long double (signed) char + {{{{{{{{{{ ? n { n { n { n {{ { unsigned char ? {{{{{{{{{ (signed) short int + { {{{{{{ ? nn { n {{ { unsigned short int ?? {{{{{ (signed) int + { {{{{{{ ? nn { n {{ { unsigned int ?? {{{{{ (signed) long int + {{{{ ? n {{ { unsigned long int ? {{ { float {{ double long double remarks 1 the signed keyword may be omitted. however, with a char type data, the data type is regarded as the signed char or unsigned char type depending on the condition (option) for compilation. 2 legend: { : type conversion will be performed properly. \: type conversion will not be performed. n: a correct value will not be generated. (the data type will be regarded as an unsigned int type.) ? : the data type will not change bit-image-wise. however, if a positive number cannot represent it sufficiently, no correct value will be generated (regarded as an unsigned integer). blank: an overflow in the result of the conversion will be truncated. the + or ? sign of the data may be changed depending on the type after the conversion.
chapter 4 t ype conversions user ? s manual u15556ej1v0um 67 4.1 arithmetic operands (1) characters and integers (general integral promotion) the data types of char, short int, and int bit fields (whether they are signed or unsigned) or of objects that have an enumeration type will be converted to int types if their values are within the range that can be represented with int types. if not within the range, they will be converted to unsigned int types. these implicit type conversions are referred to as ? general integral general promotion ? . all other arithmetic types will not be changed by this general integral promotion. in general integral promotion, the value of the original data type is retained, including its sign. char type data without a type qualifier will normally be handled as signed char in this compiler. if can also be handled as unsigned char by using an option. (2) signed integers and unsigned integers when a value with an integer type is converted to another, the value will not be changed if the value can be expressed by the integer type after conversion. when a signed integer is converted to an unsigned integer of the same or larger size, the value is not changed unless the value of the signed integer is negative. if the value of the signed integer is negative and the unsigned integer has a size larger than that of the signed integer, the signed integer is expanded to the signed integer with the same size as the unsigned integer, and then it is added to the value equal to the maximum number that can be expressed with the unsigned integer plus 1, and the signed integer before conversion is converted to the unsigned value. when a value with an integer type is converted to an unsigned integer with a smaller size, the conversion result is the non-negative remainder of the value divided with that value which 1 is added to the maximum number that can be expressed with an unsigned integer after conversion. when a value with an integer type is converted to a signed integer with smaller size or when an unsigned integer is converted to a signed integer with the same size, the overflowed value is ignored if the value after conversion cannot be expressed. for the conversion pattern, refer to table 4-1 . list of conversions between types. conversion operations from signed integral type to unsigned integral type are as listed in table 4-2 below. table 4-2. conversions from signed integral type to unsigned integral type unsigned smaller in value range greater in value range +/ { signed ? /+ { :type conversion will be performed properly. +: the data will be converted to a positive integer. /: the result of the conversion will be the remainder of the integer value, modulo the largest possible value of the type to be converted plus 1.
chapter 4 t ype conversions user?s manual u15556ej1v0um 68 (3) usual arithmetic type conversions types obtained as a result of operations on arithmetic type data have a wide range of values. the type conversion of the operation result is performed as follows.  if either one of the operands has long double type, the other operand is converted to long double type.  if either one of the operands has double type, the other operand is converted to double type.  if either one of the operands has float type, the other operand is converted to float type. in cases other than above, general integer expansion is performed for both operands according to the following rules. figure 4-1 shows the rules. figure 4-1. usual arithmetic type conversions unsigned long int unsigned int int long int if either of the two operands is unsigned long int type, or if one operand is long int type and the other is unsigned int type and the value of unsigned int type cannot be represented by long int type. both operands will be converted to unsigned long int type. in cases other than above, if one operand is long int type and if the value of the other operand can be represented by long int type, the other operand will be converted to long int type. in cases other than above, if one operand is unsigned int type, the other operand will be converted to unsigned int type. in cases other than above, both operands will have int type. in this compiler, the conversion to int type can be intentionally disabled by a compile condition (optimizing option) (for details, refer to cc78k4 c compiler operation user?s manual (u15557e) chapter 5 compiler options ).
chapter 4 t ype conversions user ? s manual u15556ej1v0um 69 4.2 other operands (1) left-side values and function locators a left-side value refers to an expression that specifies an object (and has an incomplete type other than object type or void type). left-side values that do not have array types, incomplete types, or const qualifier types, and structures or unions that have no const qualifier type members are ? modifiable left-side values ? . a left-side value that has no array type will be converted to a value stored in the object to be specified, except when it is the operand of the sizeof operator, unary & operator, ++ operator, or - - operator or the left operand of an operator or an assignment operator. by being converted, it will no longer serve as a left-side value. the behavior of left-side values that have incomplete types but have no array types is not guaranteed. a left-side value that has an ? ... array ? type except character arrays will be converted to an expression that has a ? pointer to ... ? type. this expression is no longer a left-side value. a function locator is an expression that has a function type. with the exception of the operand of the sizeof operator or unary & operator, a function locator that has a ? function type that returns ... ? will be converted to an expression that has a ? pointer type to a function that returns ... ? . (2) void the value (non-existent) of a void expression (i.e., an expression that has the void type) cannot be used in any way. neither implicit nor explicit conversion to exclude void will be applied to this expression. if an expression of another type appears in a context that requires a void expression, the value of the expression or specifier is assumed to be non-existent. (3) pointers a void pointer can be converted to a pointer to any incomplete type or object type. conversely, a pointer to any incomplete type or object type can be converted to a void pointer. in either case, the result value must be equal to that of the original pointer. an integer constant expression that has the value of 0 and has been cast to the void * type is referred to as a ? null pointer constant ? . if the null pointer constant is substituted with, equal to, or compared with some pointer, the null pointer constant will be converted to that pointer.
user?s manual u15556ej1v0um 70 chapter 5 operators and expressions this chapter describes the operators and expressions to be used in the c language. c has an abundance of operators for arithmetic, logical, and other operations. this rich set of operators also includes those for bit and address operations. an expression is a string or combination of an operator and one or more operands. the operator defines the action to be performed on the operand(s) such as computation of a value, instructions on an object or function, generation of side effects, or a combination of these. examples of operators are given below. #define true 1 #define false 0 #define size 200 void lprintf(char *, int); void putchar(char c); char mark [size+1]; + ................................................................... arithmetic operator void main(void) { int i, prime, k, count; count = 0; =.............................................................. assignment operator for (i = 0 ; i <= size ; i++) ++ ............................................. postfix operator mark [i] = true; <= ............................................. relational operator for (i = 0 ; i <= size ; i++) { if (mark [i]) { prime = i + i + 3; + ............................................... arithmetic operator lprintf (?%d? , prime); count++; ++ ............................................. postfix operator if ((count%8) == 0) ==............................. relational operator putchar ('\n'); for (k = i + prime ; k<=size; k += prime) += ...... assignment operator mark [k] = false; } }
chapter 5 operators and expressions user ? s manual u15556ej1v0um 71 lprintf( ? total %d\n ? , count); loop1: goto loop1; } lprintf(char *s, int;) { int j; char *ss; j = i; ss = s; } void puttchar(char c){ char d; d = c; }
chapter 5 operators and expressions user ? s manual u15556ej1v0um 72 table 5-1 shows the evaluation priority of operators used in c. table 5-1. evaluation precedence of operators type of expression operator linkage priority postfix [ ] ( ) . ? > ++ ? ? highest unary ++ ? ? & * + ? ~ ! sizeof cast (type) multiplicative * / % additive + ? bitwise shift << >> relational < > <= >= equality == != bitwise and & bitwise xor ^ bitwise or | logical and && logical or | | conditional ? : assignment = *= /= %= += ? = <<= >>= &= ^= | = comma , lowest the arrow ( or ) in the ? linkage ? column denotes that when an expression contains two or more operators of the same priority, the operations are carried out in the direction of the arrow ? ? (from left to right) or ? ? (from right to left).
chapter 5 operators and expressions user ? s manual u15556ej1v0um 73 5.1 primary expressions primary expressions include the following.  identifier declared as object or function (identifier primary expression)  constant (constant primary expression)  string literal (constant primary expression)  expression enclosed in parentheses (parenthesized expression) an identifier that becomes a primary expression is a left-side value if an object is declared or a function locator if a function is declared. the data type of a constant is determined according to the value specified for the constant as explained in 2.4 constants . string literal(s) become a left-side value that has a data type as explained in 2.5 string literals . 5.2 postfix operators a postfix operator is an operator that appears or is placed after an object or function. the primary expressions are described below.
chapter 5 operators and expressions user ? s manual u15556ej1v0um 74 (1) subscript operators postfix operators [ ] subscript operator function the [ ] subscript operator specifies or refers to a single member of an array object. the array or expression ? e1 [e2] ? is evaluated as if it were ? (*(e1+(e2))) ? . in other words, the value of e1 is a pointer to the first member of the array and e2 (if it is an integer) indicates the e2th member of e1 (counting from 0). with a multidimensional array, as many subscript operators as the number of dimensions must be connected. in the following example, x becomes an int type array of 3*5. in other words, x is an array which has three members each consisting of five int type members. int x[3][5] ; a multidimensional array may be specified by connecting subscript operators. assuming that e is an array of nth dimension (where n 2) consisting of i*j*...*k, the array can be specified with n number of subscript operators. in this case, e becomes a pointer to an array of (n ? 1)th dimension consisting of j*...*k. syntax postfix-expression [subscripted expression] note a postfix expression must have a ? .... pointer to object ? . the subscripted expression of an array must be specified with integral type data. the result of the expression will become ? ..... ? type.
chapter 5 operators and expressions user ? s manual u15556ej1v0um 75 (2) function call operators postfix operators ( ) function call function the postfix ( ) operator calls a function. the function to be called is specified with a postfix expression and argument(s) to be passed to the function are indicated in parentheses ( ). the description related to function includes the function prototype declaration, the function definition (the body of a function), and the function call. the function prototype declaration specifies the value a function returns, the type of argument, and the storage class. if the function prototype declaration is not referred to in a function call, each argument is extended with a general integer. this is called ? default actual argument extension ? . performing a function prototype declaration avoids default actual argument extension and detects errors in of the type and number of arguments and the type of return value. calling a function that has neither a storage class specification nor a data type specification such as ? identifier ( ); ? is interpreted as calling a function that has an external object and returns an int type that has no information on arguments. in other words, the following declaration will be made implicitly. extern int identifier ( ) ; syntax postfix-expression ( argument-expression list ); [example of function call] int func (char, int); /* function prototype declaration */ char a ; int b, ret; void main(void){ ret = func(a, b); /* function call */ } int func(char c, int i) { /* function definition */ : return i; } note a function that returns an object other than array types can be called with this operator. the postfix expression must be of a pointer type to this function. in a function call including a prototype, the type of argument must be of a type that can be assigned to the corresponding parameter(s). the number of arguments must also be in agreement.
chapter 5 operators and expressions user ? s manual u15556ej1v0um 76 (3) structure and union member postfix operators . -> <1> . (dot) operator function the . (dot) operator (also called a member operator) specifies the individual members of a structure or union. the postfix expression is the name of the structure or union object to be specified, and the identifier is the name of the member. syntax postfix-expression . identifier <2> (arrow) operator function the (arrow) operator (also called an indirect membership operator) specifies the individual members of a structure or union. the postfix expression is the name of the pointer to the structure or union object to be specified, and the identifier is the name of the member. syntax postfix-expression identifier
chapter 5 operators and expressions user ? s manual u15556ej1v0um 77 postfix operators . -> [examples of ?.?, ?->? operators] #include union { struct { int type ; } n ; struct { int type ; int intnode ; } ni ; struct { int type ; struct { long longnode ; } *nl_p ; } nl ; } u ; void func (void) { u. nl. type = 1 ; u. nl.nl_p -> longnode = -31415l ; /*...*/ if (u.n.type = = 1) u.nl.nl_p -> longnode = labs (u. nl.nl_p -> longnode) ; }
chapter 5 operators and expressions user ? s manual u15556ej1v0um 78 (4) postfix increment/decrement operators postfix operators ++ ? ? <1> postfix ++ (increment) operator function the postfix ++ (increment) operator increments the value of an object by 1. this increment operation is performed taking the data type of the object into account. syntax postfix-expression ++ note see <2> below. <2> postfix ? ? (decrement) operator function the postfix ? ? (decrement) operator decrements the value of an object by 1. this decrement operation is performed taking the data type of the object into account. syntax postfix expression ? ? note the operand of the postfix increment or decrement operator must be a modifiable left-side value (qualified or unqualified).
chapter 5 operators and expressions user?s manual u15556ej1v0um 79 5.3 unary operators a unary operator performs an operation on one object or parameter (i.e., operand). the following unary operators are available.  prefix increment and decrement operators + + ? ?  address and indirect operators & *  unary arithmetic operators + ? ~ !  sizeof operator sizeof the followings explain each unary operators are described below.
chapter 5 operators and expressions user?s manual u15556ej1v0um 80 (1) prefix increment and decrement operators unary operators ++ ? ? <1> prefix ++ (increment) operator function the prefix ++ (increment) operator increments the value of an object by 1. the expression ?++e? of the prefix increment operator will produce the same result as the following expression. e = e + 1 or e+ = 1 syntax + + unary-expression <2> prefix ? ? (decrement) operator function the prefix ? ? (decrement) operator decrements the value of an object by 1. the expression ?? ?e? of the prefix decrement operator will produce the same result as the following expression. e = e ? 1 or e ? = 1 syntax ? ? unary-expression
chapter 5 operators and expressions user?s manual u15556ej1v0um 81 (2) address and indirection operators unary operators & * <1> unary & operator function the unary & (address) operator returns the pointer of a specified object (i.e., the address of the variable it precedes). syntax & operand <2> unary * operator function the unary * (indirection) operator returns the value indicated by a specified pointer (i.e., takes the value of the variable it precedes and uses that value as the address of the information in memory). syntax * operand note the operand of the unary & operator must be a left-side value referring to an object not declared with the register storage class specifier. neither a function locator nor a bit field can be used as the operand of this unary operator. the operand of the unary * operator must have a pointer type.
chapter 5 operators and expressions user?s manual u15556ej1v0um 82 (3) unary arithmetic operators (+ ? ~ !) unary operators + ? ~ ! functions the + (unary plus) operator performs positive integral promotion on its operand. the ? (unary minus) operator performs negative integral promotion on its operand. the ~ (tilde) operator is a bitwise one?s complement operator which inverts all the bits in a byte of its operand. the ! not or logical negation operator returns 0 if its operand is 0 and 1 if it is not 0. in other words, the operator changes each 0 to 1 and 1 to 0. syntax + operand ? operand ~ operand ! operand
chapter 5 operators and expressions user?s manual u15556ej1v0um 83 (4) sizeof operators unary operators sizeof operator function the sizeof operator returns the size of a specified object in bytes. the return value is governed by the data type of the object and the value of the object itself is not evaluated. the value to be returned by an unsigned char or signed char object (including its qualified type) on which a sizeof operation is performed is 1. with an array type object, the return value will be the total number of bytes in the array. with a structure or union type object, the result value will be the total number of bytes that the object would occupy including bytes necessary to pad out to the next appropriate alignment boundary. the type of the sizeof operation result is an integral type and its name is size_t. this name is defined in the header. the sizeof operator is used mainly to allocate memory areas and transfer data to/from the i/o system. syntax sizeof unary-expression or sizeof (type-name) example the following example finds the number of elements of an array by dividing the total number of bytes in the array by the size of a single element. num becomes 5. int num; char array[ ]= {0, 1, 2, 3, 4}; void func(void){ num = sizeof array / sizeof array [0] ; } note an expression that has a function type or incomplete type and a left-side value that refers to a bit field object cannot be used as the operand of this operator.
chapter 5 operators and expressions user?s manual u15556ej1v0um 84 5.4 cast operators a cast is a special operator that forces one data type to be converted into another. the cast operator is mainly used when converting a pointer type. cast operators (type-name) function the cast operator converts the data type of another object (or the result of another expression) into the type specified in parentheses ( ). syntax ( type-name ) expression example void func (void) { int val; float f; f = 3.14f; val = (int) f; /* val becomes 3 by cast */ val = *(int *)0x10000; /* cast constant */ }
chapter 5 operators and expressions user?s manual u15556ej1v0um 85 5.5 arithmetic operators arithmetic operators are divided into multiplicative operators and additive operators. multiplicative operators find the product, quotient, and remainder of two operands. additive operators find the sum and difference of two operands.  multiplicative operators * / %  additive operators + ? table 5-2. signs of division/remainder division operation result a/b b a % b b +? +? ++? +++ a ??+ a ??? remark a and b indicate operands. division is performed with two integers whose sign, if any, is removed through the usual arithmetic conversion and the result will be truncated towards 0 if necessary. likewise, a remainder or modulo division operation is performed with two integers whose sign, if any, is removed through the usual arithmetic conversion. table 5-2 shows the results of calculations only on the signs of two operands in division and remainder division, respectively. multiplicative operators and additive operators are described below. e1 and e2 used in the explanation of syntax indicate operands or expressions.
chapter 5 operators and expressions user?s manual u15556ej1v0um 86 (1) multiplicative operators multiplicative operators * / % <1> * operator function the binary * (multiplication) operator performs normal multiplication on two operands and returns the product. syntax e1 * e2 <2> / operator function the / operator performs normal division on two operands and returns the quotient. syntax e1 / e2 <3> % operator function the % operator performs a remainder (or modulo division) operation on two operands and returns the remainder in the result. syntax e1 % e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 87 (2) additive operators additive operators + ? <1> + operator function the + operator performs addition on two operands and returns the sum of the two numbers. syntax e1 + e2 <2> ? operator function the ? operator performs subtraction on two operands and returns the difference between the two numbers (the first operand minus the second operand). syntax e1 ? e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 88 5.6 bitwise shift operators a shift operator shifts its first (left) operand to the direction (left or right) indicated by the operator by the number of bits specified by its second operand. there are the following two shift operators.  shift operator << >> table 5-3. shift operations a<>b b note +0 +0 ?0 ??1 note the table indicates when the right operand is greater than the number of bits in the left operand or when an overflow occurs in the result of the shift operation. if the right operand is negative, the value is processed as an unsigned positive number. remark a and b indicate operands. the shift operators are described below. e1 and e2 indicate operands or expressions. a a
chapter 5 operators and expressions user?s manual u15556ej1v0um 89 shift operators << >> <1> left shift (<<) operators function the binary << (left shift) operator shifts the left operand to the left the number of bits specified by the right operand and fills zeros in vacated bits. if the left operand e1 has an unsigned type in ?e1 << e2?, the result will become a value obtained by multiplying e1 by the e2th power of 2. syntax e1 << e2 <2> right shift (>>) operators function the binary >> (right shift) operator shifts the left operand to the right the number of bits specified by the right operand. if the left operand is unsigned, zeros are filled in vacated bits (logical shift). if the left operand is signed, a copy of the sign bit is filled in vacated bits. if the left operand e1 is unsigned or signed and has a non-negative value in ?e1>>e2?, the result will become a value obtained by dividing e1 by the e2th power of 2. syntax e1 >> e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 90 5.7 relational operators there are two types of operators to indicate the relationship between two operands: ?relational operator? and ?equality operator?. the relational operator indicates the value relationship between two operands such as greater than and less than. the equality operators indicate that two operands are equal or not equal. the relational operators and equality operators are shown below.  relational operator < > <= >=  equality operator == ! = the value relationship between two pointers compared by relational operators is determined by the relative location in the address space of the object indicated by the pointer. in this compiler, relational operators and equality operators generate ?1? if the specified relationship is true and ?0? if it is false. the results have int type. the relational operators and equality operators are described below. e1 and e2 used in the explanation of syntax indicate operands or expressions.
chapter 5 operators and expressions user?s manual u15556ej1v0um 91 (1) relational operators relational operators < > <= >= <1> < (less than) operator function the < (less than) operator returns 1 if the left operand is less than the right operand; otherwise, 0 is returned. syntax e1 < e2 <2> > (greater than) operator function the > (greater than) operator returns 1 if the left operand is greater than the right operand; otherwise, 0 is returned. syntax e1 > e2 <3> <= (less than or equal) operator function the <= (less than or equal) operator returns 1 if the left operand is less than or equal to the right operand; otherwise, 0 is returned. syntax e1 <= e2 <4> >= (greater than or equal) operator function the >= (greater than or equal) operator returns 1 if the left operand is greater than or equal to the right operand; otherwise, 0 is returned. syntax e1 >= e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 92 (2) equality operators equality operators = = != <1> = = (equal) operator function the = = (equal) operator returns 1 if its two operands are equal to each other; otherwise, 0 is returned. syntax e1 == e2 <2> != (not equal) operator function the != (not equal) operator returns 1 if the operands are not equal to each other; otherwise, 0 is returned. syntax e1 != e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 93 5.8 bitwise logical operators bitwise logical operators perform a specified logical operation on the value of an object in bit units. the bitwise logical expressions include bitwise and (&), bitwise exclusive or ( ^ ), and bitwise inclusive or ( | ). each logical operation is indicated by the operators shown below.  bitwise and operator &  bitwise xor operator ^  bitwise or operator | the bitwise logical operators are described below. e1 and e2 used in the explanation of syntax indicate operands or expressions.
chapter 5 operators and expressions user?s manual u15556ej1v0um 94 (1) bitwise and operators bitwise and operators & function the binary & operator is a bitwise and operator that returns an integral value that has ?1? bits in positions where both operands have ?1? bits and that has ?0? bits everywhere else. the bitwise and operator must be specified with an ?operator?. table 5-4. bitwise and operation value of each bit in left operand 10 11 0 00 0 syntax e1 & e2 value of each bit in right operand
chapter 5 operators and expressions user?s manual u15556ej1v0um 95 (2) bitwise xor operators bitwise xor operators ^ function the binary ^ (caret) operator is a bitwise exclusive or operator that returns an integral value that has a ?1? bit in each position where exactly one of the operands has a ?1? bit and that has a ?0? bit in each position where both operands have a ?1? bit or both have a ?0? bit. table 5-5. bitwise xor operation value of each bit in left operand 10 10 1 01 0 syntax e1 ^ e2 value of each bit in right operand
chapter 5 operators and expressions user?s manual u15556ej1v0um 96 (3) bitwise inclusive or operators bitwise inclusive or operators | function the binary | operator is a bitwise inclusive or operator that returns an integral value that has a ?1? bit in each position where at least one of the operands has a ?1? bit and that has a ?0? bit in each position where both operands have a ?0? bit. table 5-6. bitwise or operation value of each bit in left operand 10 11 1 01 0 syntax e1 | e2 value of each bit in right operand
chapter 5 operators and expressions user?s manual u15556ej1v0um 97 5.9 logical operators logical operators perform logical or and logical and operations. a logical or operation is specified with a logical or operator, and a logical and operation is specified with a logical and operator. each operator is shown below.  logical and operator & &  logical or operator | | each operand of both the operators returns the value of int type ?0? or ?1?. the following explains each logical operator. e1 and e2 used in the explanation of syntax indicate operands or expressions.
chapter 5 operators and expressions user?s manual u15556ej1v0um 98 (1) logical and operators logical and operators && function the && operator performs a logical and operation on two operands and returns a ?1? if both operands have nonzero values. otherwise, a ?0? is returned. the type of the result is int . table 5-7. logical and operation value of left operand zero nonzero zero 0 0 nonzero 0 1 syntax e1 && e2 note this operator always evaluates its operands from left to right. if the value of the left operand is ?0?, the right operand is not evaluated. value of right operand
chapter 5 operators and expressions user?s manual u15556ej1v0um 99 (2) logical or operators logical or operators | | function the | | operator performs a logical or operation on two operands and returns a ?0? if both operands are zero. otherwise, a ?1? is returned. the type of result is int . table 5-8. logical or operation value of left operand zero nonzero zero 0 1 nonzero 1 1 syntax e1 || e2 note this operator always evaluates its operands from left to right. if the value of the left operand is nonzero, the right operand is not evaluated. value of each bit in right operand
chapter 5 operators and expressions user?s manual u15556ej1v0um 100 5.10 conditional operators conditional operators judge the processing to be performed next by the value of the first operand. conditional operators judge by ??? and ?:?. the conditional operators are described below. (1) conditional operators (?, :) conditional operators ? : function the conditional (?, :) operator evaluates the first operand before the ?. if the value of the first operand is nonzero, it evaluates the second operand before the colon. if the value of the first operand is zero, it evaluates the third operand after the colon. the result of the entire conditional expression will be the value of the second or third operand. syntax 1st-operand ? 2nd-operand : 3rd-operand example #define true 1 #define false 0 char flag ; int ret ; ret func () { ret = flag ? true : false ; return ret ; } note if both the second and third operand types are arithmetic types, normal arithmetic type conversion is performed to make them common types. the type of result is the common type. if both the operand types are structure types or union types, the result becomes those types. if both the operand types are void types, the result is a void type.
chapter 5 operators and expressions user?s manual u15556ej1v0um 101 5.11 assignment operators assignment operators include a simple assignment expression that stores the right operand in the left operand and a compound assignment expression that stores the result of an operation on both operands in the left operand. the assignment operators are shown below.  assignment operators = *= /= %= += ?= <<= >>= &= ^= |= the each assignment operators are described below. e1 and e2 used in the explanation of syntax indicate operands or expressions.
chapter 5 operators and expressions user?s manual u15556ej1v0um 102 (1) simple assignment operators simple assignment operators = function the = (simple assignment) operator converts the right operand (expression) to the type of the left operand (left- side value) before the value is stored. in the following example, the value of an int type to be returned from the function by the type conversion of the simple assignment expression will be converted to a char type and an overflow in the result will be truncated. the comparison of the value with ??1? will be made after the value is converted back to the int type. if the variable ?c? declared without a qualifier is not interpreted as unsigned char , the result of the variable will not become negative and its comparison with ??1? will never result in equal. in such a case, the variable ?c? must be declared with an int type to ensure complete portability. int f(void) ; char c ; /*...*/ ((c = f ()) == -1) /*...*/ syntax e1 = e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 103 (2) compound assignment operators compound assignment operators *= /= %= += ?= <<= >>= &= ^= |= <1> compound assignment operators function the compound assignment operators perform a specified operation on both operands and stores the result in the left operand. the value to be stored in the left operand will be converted to the type of the left-side value (left operand). the compound assignment expression ?e1 op = e2? (where op indicates a suitable binary operator) is equivalent to the simple assignment expression ?e1 = e1 op (e2)?, except that the left-side value (e1) is only evaluated once. the following compound assignment expressions will produce the same result as the respective simple assignment expressions on the right. a *= b; a = a * b; a /= b; a = a / b; a %= b; a = a % b; a += b; a = a + b; a ?= b; a = a ? b; a <<= b; a = a << b; a >>= b; a = a >> b; a &= b; a = a & b; a ^= b; a = a ^ b; a |= b; a = a | b; syntax e1 *= e2 e1 /= e2 e1 %= e2 e1 += e2 e1 ?= e2 e1 <<= e2 e1 >>= e2 e1 &= e2 e1 ^= e2 e1 |= e2
chapter 5 operators and expressions user?s manual u15556ej1v0um 104 5.12 comma operator (1) comma operator comma operator , function the comma operator evaluates the left operand as a void type (that is, ignores its value) and then evaluates the right operand. the type and value of the result of the comma expression are the type and value of the right operand. in contents where a comma has another meaning (as in a list of function arguments or in a list of variable initializations), comma expressions must be enclosed in parentheses. in other words, the comma operator described in this chapter will not appear in such a list. in the following example, the comma operator finds the value of the second argument of the function ?f ( )?. the value of the second argument becomes 5. int a, c, t; void main(void) f(a, (t=3, t+2), c); } syntax e1 , e2
chapter 5 operators and expressions user ? s manual u15556ej1v0um 105 5.13 constant expressions constant expressions include general integral constant expressions, arithmetic constant expressions, address constant expressions, and initialization constant expressions. most of these constant expressions can be calculated at translation instead of execution. in a constant expression, the following operators cannot be used except when they appear inside sizeof expressions.  assignment operators  increment operators  decrement operators  function call operator  comma operator (1) general integral constant expression a general integral constant expression has a general integral type. the following operands may be used.  integer constants  enumerated value constants  character constants  sizeof expressions  floating-point constants (2) arithmetic constant expression an arithmetic constant expression has an integral type. the following operands may be used.  integer constants  enumerated value constants  character constants  sizeof expressions  floating-point constants (3) address constant expression an address constant expression is a pointer to an object that has a static storage duration or a pointer to a function locator. such an expression must be created explicitly using the unary & operator or implicitly using an expression with an array type or function type. the following operands may be used.  array subscript operator [ ]  . (dot) operator  (arrow) operator  & address operator  * indirection operator  pointer casts however, none of these operators can be used to access the value of an object.
user?s manual u15556ej1v0um 106 chapter 6 control structures of c language this chapter describes the program control structures of c language and the statements to be executed in c. generally speaking, no matter how complicated a process is, it can be expressed with three basic control structures. these three control structures are: sequential, conditional (selection), and iteration. branch is used to change the flow of a program by force. (1) sequential processing statements in a program are executed one by one from top to bottom in the order of their description in the program. (2) conditional (selection) processing according to the status of the program under execution, the next executable statement is selected and executed. the selection condition is specified in a control statement. the control statement determines which of the two alternative statement groups or multiway (three or more) alternative statement groups is to be executed. (3) looping (iteration) processing the same processing is executed two or more times. the execution of an executable statement is repeated a specified number of times in the state indicated by the control statement. (4) branch processing c is forced to exit from the current program flow and control is transferred to a specified label. program execution starts from the statement next to the specified label. there are six types of statements used in c.  labeled statement ............................... causes branch according to the value of the switch statement and the destination of the goto statement  compound statement (block) .............. collects two or more statements to be processed as one unit  expression statement .......................... a statement consisting of an expression and a semicolon  selection statement ............................. selects a statement out of several statements according to the value of the expression  iteration statement............................... repeatedly performs a statement called the body of a loop until control expression becomes equal to 0  branch statement ................................ causes an unconditional branch to a different destination
chapter 6 control structures of c language user ? s manual u15556ej1v0um 107 a description example of these statements is shown below. [description example] #define size 10 #define true 1 #define false 0 extern void putchar(char) ; extern void lprintf(char*, int) ; char mark [size+1]; void main (void) { int i, prime, k, count; count = 0 ; for (i = 0 ; i <= size ; i++) ............................ /* for .............. iteration statement */ mark [i] = true ; for (i = 0 ; i <= size ; i++) {....................... / * for ............. iteration statement */ if (mark [i]) { ............................................ / * if ............... selection statement */ prime = i + i + 3; lprintf (?%d? , prime); if ((count%8) == 0) putchar ('\n'); for (k = i + prime ; k <= size ; k += prime) / * if ............... selection statement * / mark [k] = false; } } lprintf (?total %d\n?, count); loop1; ............................................ / * loop1: ....... labeled statement * / goto loop1; ............................................ / * goto .......... branch statement * / }
chapter 6 control structures of c language user ? s manual u15556ej1v0um 108 6.1 labeled statements a labeled statement specifies the destination of the switch or goto statement. the switch statement selects the statement specified by a control expression from among statements with two or more options. the labeled statement becomes the label of the statement to be executed by the switch statement. the goto statement causes unconditional branching to the applicable label from the normal flow of processing. the syntax of labeled statements is given below.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 109 (1) case label labeled statements case label function case labels are used only in the body of a switch statement to enumerate values to be taken by the control expression of the switch statement. syntax case constant-expression : statement example 1 int f (void), i; void main (void) { /* ... */ switch (f()) { case 1: i = i + 4 ; break ; case 2: i = i + 3 ; break ; case 3: i = i + 2 ; } /* ... */ } explanation in example 1, if the return value of f( ) is 1, the first case clause (statement) is selected and the expression ? i=i+4 ? is executed. likewise, if the return value of f( ) is 2 or 3, the second or third case statement is selected, respectively. each break statement in the above example is to break out of the switch body. as in this example, case labels are used when two or more options are involved.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 110 labeled statements case label example 2 int i ; void main (void) { /* ... */ i = 2 ; switch(i) { case 1: i = i + 4 ; case 2: i = i + 3 ; case 3: i = i + 2 ; } /* ... */ } explanation in example 2, the processing starts in the second case statement since i is 2. the third statement is also consecutively performed since the case statement does not include a break statement. thus, if the constant expression and the control expression in the case statement match, the programs thereafter are performed sequentially. a break statement is used to exit the switch statement.
chapter 6 control structures of c language user?s manual u15556ej1v0um 111 (2) default label labeled statements default label function a default label is a special case label used only in the body of a switch statement to specify a process to be executed by c if the value of the control expression does not match any of the case constants. syntax default : statement example int f (void), i; switch (f()) { case 1: i = i + 4 ; break ; case 2: i = i + 3 ; break ; case 3: i = i +2 ; default: i = 1; } explanation in the above example, if the return value of f( ) is 1, 2, or 3, the corresponding case clause (statement) is selected and the expression that follows the case label is executed. each break statement in the above example is used to break out of the switch body. if the return value of f( ) is other than 1 to 3, the expression that follows the default label is executed. in this case, the value of i becomes 1.
chapter 6 control structures of c language user?s manual u15556ej1v0um 112 6.2 compound statements or blocks a compound statement or block consists of two or more statements grouped together with enclosing braces and executed as one unit syntax-wise. in other words, by enclosing zero or more declarations followed by zero or more statements all in braces, these statements can be processed as a compound statement whenever a single statement is expected. 6.3 expression statements and null statements an expression statement consists of a statement and a semicolon. a null statement consists of only a semicolon and is used for labels that require a statement and in looping that does not need a body. the description examples of expression statements and null statements are given below. as in the following example, for a function to be called as an expression statement merely to obtain side effects, the value of its return value can be discarded by using a cast expression. int p(int) ; void main(void) { /*...*/ (void)p(0) ; } a null statement can be used as the body of a looping statement as shown below. char *s ; void main(void) { /*...*/ while (*s++ != '0') ; /* */ } in addition, it can be used to place a label before a brace ( } ) that closes a compound statement as shown below. void func(void){ /*...*/ while (loop1) { /*...*/ while (loop2) { /*...*/ if (want_out) goto end_loop1 ; /*...*/ } end_loop1: ; } }
chapter 6 control structures of c language user?s manual u15556ej1v0um 113 6.4 conditional statements conditional (or selection) statements include the if and switch statements. the if or switch statement allows the program to choose one of several groups of statements to execute, based on the value of the control expression enclosed in parentheses. the control flows of if and switch statements are illustrated in figure 6-1 below. figure 6-1. control flows of conditional statements control flow of switch statement switch case 1 case 2 case 3 default : control flow of if statement if condition executes statement that follows if . executes statement that follows else . false true
chapter 6 control structures of c language user ? s manual u15556ej1v0um 114 (1) if and if ... else statements conditional statements if, if ... else function an if statement has a one-way selection structure and executes the statement that follows the control expression enclosed in parentheses if the value of the control expression is nonzero (true). an if ... else statement has a two-way selection structure and executes the statement-1 that follows the control expression if the value of the control expression is nonzero (true) or the statement-2 that follows else if the value of the control expression is zero (false). syntax if (expression) statement if (expression) statement-1 else statement-2 example unsigned char uc ; void func (void){ if ( uc < 10 ){ /*111*/ } else{ /* 222 */ } } explanation in the above example, if the value of uc is less than 10 based on the control expression in the if statement, the block ? {/*111*/} ? is executed. if the value is greater than 10, the block ? {/*222*/} ? is executed. note when the processing after the if statement/ if...else statement is not enclosed with ? { } ? , only the processing of a line after the if statement/ if...else statement is performed regarding it as the body.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 115 (2) switch statement conditional statements switch function a switch statement has a multiway branching structure and passes control to one of a series of statements that have the case labels in the switch body depending on the value of the control expression enclosed in parentheses. if no case label that corresponds to the control expression exists, the statement that follows the default label is executed. if no default label exists, no statement is executed. syntax switch (expression) statement example extern void func(void); unsigned char mode ; void main(void) { switch (mode) { case 2: mode = 8 ; break ; case 4: mode = 2 ; break ; case 8: func( ); } } note the same value cannot be set in each case label in the switch body. only one default label can be used in the switch body.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 116 6.5 iteration statements an iteration statement executes a group of statements in the loop body as long as the value of the control expression enclosed in parentheses is true (nonzero). c has the following three types of iteration statements. ? while statement  do statement  for statement the control flow of each type of iteration statement is illustrated in figure 6-2 below. figure 6-2. control flows of iteration statements while condition executes statement (s) that follow while . false true control flow of while loop loop control flow of do-while loop while condition executes statement (s) that follow do . loop true false control flow of for loop for condition reevaluates control expression. false true loop executes statement (s) that follow for . initialize
chapter 6 control structures of c language user ? s manual u15556ej1v0um 117 (1) while statement iteration statements while statement function a while statement executes one or more statements (the body of the while loop) several times as long as the value of the control expression enclosed in parentheses is true (nonzero). the while statement evaluates the control expression before executing its loop body. syntax while (expression) statements example int i, x ; void main (void) { i=1, x=0 ; while ( i < 11 ) { x += i ; i++ ; } } explanation the above example finds the sum total of integers from 1 to 10 for x. the two statements enclosed in braces are the body of this while loop. the control expression ? i<11 ? returns 0 if the value of i becomes 11. for this reason, the loop body is executed repeatedly as long as the value of i is less than 11 (between 1 and 10). ? while(1) {statement} ? is used to endlessly perform a loop statement.
chapter 6 control structures of c language user?s manual u15556ej1v0um 118 (2) do statement iteration statements do statement function a do statement executes the body of the loop as long as the control expression enclosed in parentheses is true (nonzero). the do statement evaluates the control expression after the loop body has been executed. syntax do statements while ( expression ) ; example int i, x ; void main (void) { i=1, x=0 ; do { x += i ; i++ ; } while( i<11 ); } explanation the above example finds the sum total of integers from 1 to 10 for x. the two statements enclosed in braces are the body of this do ... while loop. the control expression ?i<11? returns 0 if the value of i becomes 11. for this reason, the loop body is executed repeatedly as long as the value of i is less than 11 (between 1 and 10). the body of the loop is always performed once or more since the control expression of a do statement is evaluated after execution.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 119 (3) for statement iteration statements for statement function a for statement executes the body of the for loop a specified number of times as long as the value of the control expression is nonzero (true). of the three expressions inside the parentheses separated by semicolons, the first expression is an initializing statement to initialize a variable to be used as a counter and is executed only once in the beginning of the loop, the second is the control expression for testing the counter value, and the third is a step statement executed at the end of every loop and reevaluates the variable after the execution. syntax for ( 1st-expression ; 2nd-expression ; 3rd-expression ) statements example int i, x=0 ; for( i=1 ; i<11 ; ++i ) x += i ; explanation the above example finds the sum total of integers from 1 to 10 for x. ? x+=i ? is the body of this for loop. the control expression ? i<11 ? returns 0 if the value of i becomes 11. for this reason, the loop body is executed repeatedly as long as the value of i is less than 11 (between 1 and 10). note when the processing after for statement is not enclosed with ? { } ? , only the processing of a line after the for statements is regarded as the body of the loop of the for statement. the first and the third expression of a for statement can be omitted. when the second expression is omitted, it is replaced with a constant other than 0. the description of ? for (; ;) statement ? is used to endlessly perform the body of the loop.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 120 6.6 branch statements a branch statement is used to exit from the current control flow and transfer control to elsewhere in the program. branch statements include the following four statements.  goto statement  continue statement  break statement  return statement the control flow of each type of branch statement is shown in figure 6-3. figure 6-3. control flows of branch statements continue continue loop break break loop
chapter 6 control structures of c language user ? s manual u15556ej1v0um 121 (1) goto statement branch statements goto function a goto statement causes program execution to unconditionally jump to the label name specified in the goto statement within the current function. syntax goto identifier ; example do { /*...*/ goto point ; /*...*/ }while(/*...*/) ; /*...*/ point: ; explanation in the above example, when control is passed to the goto statement, c jumps out of the current do ... while loop processing unconditionally and transfers control to the statement next to ? point ? . note the label name (branch destination) to be specified in a goto statement must have been specified within the current function that includes the goto statement. in other words, a goto can branch only within the current function - not from one function to another.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 122 (2) continue statement branch statements continue function a continue statement is used in the body of loops in a looping statement. continue ends one cycle of the loop by transferring control to the end of the loop body. when a continue statement is enclosed by more than one loop, it ends the cycle of the smallest enclosing loop. syntax continue ; example while(/*...*/){ /*...*/ continue ; /*....*/ contin: ; } explanation in the above example, when the while loop processing by c reaches the continue statement, c unconditionally branches to the label ? contin ? . the label ? contin ? indicates the branch destination and may be omitted. the same branching operation may be performed by using ? goto contin ; ? instead of continue . note a continue statement can only be used as the body of a loop or in the body of loops.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 123 (3) break statement branch statements break function a break statement may appear in the body of a loop and in the body of a switch statement and causes control to be transferred to the statement next to the loop or switch statement. syntax break ; example int i; unsigned char count, flag; void main(void) { /* ... */ for (i = 0;i < 20;i++) { switch(count){ case 10 : flag = 1; break; /* exit switch statement */ default: func() ; } if (flag) break ; /*exit for loop */ } } explanation in the above example, break statements are used so that more than required evaluations are not performed in the body of the switch statement. if the corresponding case label is found in evaluating the switch statement, the break statement causes c to exit from the switch statement. note a break statement can only be used as the body of a looping or switch statement or in the loop or switch body.
chapter 6 control structures of c language user ? s manual u15556ej1v0um 124 (4) return statement branch statements return function a return statement exits the function that includes the return and passes controls to the function that called the return, and calls and returns the value of the return statement expression as the value of the function call expression. two or more return statements may be used in a function. using the closing brace ? } ? at the end of a function produces the same result as when a return statement without expression is executed. syntax return expression ; example int f(int); void main(void) { /*...*/ int i = 0, y = 0 ; y = f(i) ; /*...*/ } int (int i) { int x = 0 ; /*...*/ return(x) ; } explanation in the above example, when control is passed to the return statement, the function f( ) returns a value to the function main ( ) . because the value of the variable ? x ? is returned as the return value, the assignment operator causes the variable ? y ? to be substituted with the value of the variable ? x ? . note with a void type function, an expression that indicates a return value cannot be used for a return statement.
user?s manual u15556ej1v0um 125 chapter 7 structures and unions a structure or union is a collection of member objects with different types grouped under one given name. the member objects of a structure are allocated successively to memory space, while the member objects of a union share the same memory.
chapter 7 structures and unions user ? s manual u15556ej1v0um 126 7.1 structures as mentioned earlier, a structure is a collection of member objects successively allocated to memory space. (1) declaration of structure and structure variable a structure declaration list and a structure variable are declared with the keyword struct . any ? tag ? name can be given to the structure declaration list. subsequently, the structure variables of the same structure may be declared using this tag name. [declaration of structure] struct tag name structure declaration list variable name; in the following example, in the first struct declaration, int type array ? code ? , char type arrays name , addr , and tel which have the tag name ? data ? are specified and no1 is declared as the structure variable. in the second struct declaration, the structure variables no2 , no3 , no4 , and no5 that are of the same structure as no1 are declared. [example] struct data { int code; char name [12]; char addr [50]; char tel [12]; } no1; struct data no2, no3, no4, no5;
chapter 7 structures and unions user ? s manual u15556ej1v0um 127 (2) structure declaration list a structure declaration list specifies the structure of a structure type to be declared. individual elements in the structure declaration list are called members and an area is allocated for each of these members in the order of their declaration. in the following [example of structure declaration list], an area is allocated in the order of variable a, array b, and two dimensional array c. neither an incomplete type (an array of unknown size) nor a function type can be specified as the type of each member. therefore, the structure itself cannot be included in the structure declaration list. each member can have any object type other than the above two types. a bit field that specifies each member in bits can also be specified. if a variable takes a binary value ? 0 ? or ? 1 ? , the minimum required number of bits is specified as 1 for a bit field. by this specification of the minimum required number of bits with the bit field, two or more members can be stored in an integer area. [example of structure declaration list] int a; char b [7]; char c [5] [10]; [example of bit field declaration] struct bf_tag { unsigned int a:2; unsigned int b:3; bit field unsigned int c:1; } bit_field;
chapter 7 structures and unions user ? s manual u15556ej1v0um 128 (3) arrays and pointers structure variables may also be declared as an array or referenced using a pointer. [structure arrays] an array of structures is declared in the same ways as other objects. struct data{ char name [12]; char addr [50]; char tel [12]; }; struct data no [5]; [structure pointers] a pointer to a structure has the characteristics of the structure indicated by the pointer. in other words, if a structure pointer is incremented, adding the size of the structure to the pointer points to the next structure. in the following example, ? dt_p ? is a pointer to the value of ? struct data ? type. here, if the pointer ? dt_p ? is incremented, the pointer becomes the same value as ? &no[1] ? . struct data no[5]; struct data *dt_p = no;
chapter 7 structures and unions user ? s manual u15556ej1v0um 129 (4) how to refer to structure members a structure member (or structure element) may be referenced in two ways: one by using a structure variable and the other by using a pointer to a variable. [reference by using a structure variable] the . (dot) operator is used for referring to a structure member by using a structure variable. struct data { char name [12]; char addr [50]; char tel [12]; } no[5] = {?name?, ?addr?, ?tel?}; *data_ptr = no; void main(){ char c; c = no[0]. name[1]; } [reference by using a pointer to a variable] the ?> (arrow) operator is used for referring to a structure member by using a pointer to a variable. struct data { char name [12]; char addr [50]; char tel [12]; } no[5] = {?name?, ?addr?, ?tel?}, *data_ptr = no; void main(){ char c; data_ptr -> tel [3] = ?e? ; }
chapter 7 structures and unions user?s manual u15556ej1v0um 130 7.2 unions as mentioned earlier, a union is a collection of members that share the same memory space (or overlap in memory). (1) declaration of union and union variable a union declaration list and a union variable are declared with the keyword union. any ?tag? name can be given to the union declaration list. subsequently, the union variables of the same union may be declared using this tag name. [declaration of union] union tag name {union declaration list} variable name ; in the following example, in the first union declaration, char type arrays ?name?, ?addr?, and ?tel? that have the tag name ?data? are specified and ?no1? is declared as the union variable. in the second union declaration, the union variables ?no2, no3, no4, and no5?, which are of the same union as ?no1?, are declared. union data { char name [12]; char addr [50]; char tel [12]; } no1; union data no2, no3, no4, no5; (2) union declaration list a union declaration list specifies the structure of a union type to be declared. individual elements in the union declaration list are called members and an area is allocated for each of these members in the order of their declaration. in the following [example of union declaration list], an area is allocated to ?c?, which becomes the largest area of the members. the other members are not allocated new areas but use the same area. neither an incomplete type (an array of unknown size) nor a function type can be specified as the type of each member same as the union declaration list. each member can have any object type other than the above two types. [union declaration list] int a; char b [7]; char c [5] [10];
chapter 7 structures and unions user?s manual u15556ej1v0um 131 (3) union arrays and pointers union variables may also be declared as an array or referenced using a pointer (in much the same way as structure arrays and pointers). [union arrays] an array of unions is declared in the same ways as other objects. union data { char name [12]; char addr [50]; char tel [12]; }; union data no [5]; [union pointers] a pointer to a union has the characteristics of the union indicated by the pointer. in other words, if a union pointer is incremented, adding the size of the union to the pointer points to the next union. in the following example, ?dt_p? is a pointer to the value of ?union data? type. union data no[5]; union data *dt_p = no;
chapter 7 structures and unions user?s manual u15556ej1v0um 132 (4) how to refer to union members a union member (or union element) may be referenced in two ways: one by using a union variable and the other by using a pointer to a variable. [reference by using a union variable] the . (dot) operator is used for referring to a union member by using a union variable. union data { char name [12]; char addr [50]; char tel [12]; } no[5] = {?name?, ?addr?, ?tel?}; void main (void) { no[0].addr[10] = ?b? ; : } [reference by using a pointer to a variable] the ?> (arrow) operator is used for referring to a union member by using a pointer to a variable. union data { char name [12]; char addr [50]; char tel [12]; } *data_ptr ; void main(void) { data_ptr -> name[1] = ?n? ; : }
user?s manual u15556ej1v0um 133 chapter 8 external definitions in a program, lists of external declarations come after the preprocessing. these declarations are referred to as ?external declarations? because they appear outside a function and have effective file ranges. a declaration to give a name to external objects by identifiers or a declaration to secure storage for a function is called an external definition. if an identifier declared with external linkage is used in an expression (except the operand part of the sizeof operator), one external definition for the identifier must exist somewhere in the entire program. the syntax of external definitions is given below. #define true 1 #define false 0 #define size 200 void printf (char*, int); void putchar (char c); char mark[size+1]; external object declaration main() { int i, prime, k, count; count = 0; for ( i = 0 ; i <= size ; i++) mark [i] = true; for ( i = 0 ; i <= size ; i++){ if (mark[i]) { prime = i + i + 3; printf (?%d ?,prime); count++; if ( (count%8) = = 0) putchar(?\n?); for ( k = i + prime ; k <= size ; k += prime ) mark[k] = false; } } printf(?total %d\n?, count); loop1: goto loop1; }
chapter 8 external definitions user ? s manual u15556ej1v0um 134 8.1 function definition a function definition is an external definition that begins with a declaration of the function. if the storage class specifier is omitted from the declaration, extern is assumed to have been defined. an external function definition means that the defined function may be referenced from other files. for example, in a program consisting of two or more files, if a function in another file is to be referenced, this function must be defined externally. the storage class specifier of an external function is extern or static . if a function is declared as extern , the function can be referenced from another file. if declared as static , it cannot be referenced from another file. in the following example, the storage class specifier is ? extern ? and the type specifier is ? int ? . these two are default values and thus may be omitted from specification. the function declarator is ? max(int a, int b) ? and the body of the function is ? {return a>b?a:b;) ? . [example of function definition] extern int max(int a, int b) { return a>b?a :b ; } because this function definition specifies a parameter type in the function declaration, the type of argument is forcibly converted by the compiler. this type conversion can be described by using the form of an identifier list for the parameters. an example of this identifier list is shown below. extern int max(a, b) int a, b; { return a>b?a:b; } the address of a function may be passed as an argument to the function call. by using the function name in the expression, a pointer to the function can be generated. int f(void); void main( ){ : g(f); }
chapter 8 external definitions user ? s manual u15556ej1v0um 135 in the above example, the function g is passed to the function f by a pointer that points to the function f . the function g must be defined in either of the following two ways. void g (int(*funcp)(void)) { (*funcp) (); /* or funcp();*/ } or void g (int func(void)) { func(); /* or (*func)();*/ }
chapter 8 external definitions user ? s manual u15556ej1v0um 136 8.2 external object definitions an external object definition refers to the declaration of an identifier for an object that has file scope or an initializer. if the declaration of an identifier for an object that has file scope has no initializer without storage class specification or has static storage class, the object definition is considered to be temporary, because it becomes a declaration that has file scope with initializer 0. examples of external object definitions are shown below. [example of external object definition] int i1 = 1 ; ........................ definition with external linkage static int i2 = 2 ; ....... definition with internal linkage extern int i3 = 3 ; ....... definition with external linkage int i4 ; .............................. temporary definition with external linkage static int i5 ; ............... temporary definition with internal linkage int i1 ; .............................. valid temporary definition which refers to previous declaration int i2 ; .............................. violation of linkage rule int i3 ; .............................. valid temporary definition which refers to previous declaration int i4 ; .............................. valid temporary definition which refers to previous declaration int i5 ; .............................. violation of linkage rule extern int i1 ; ............... reference to previous declaration which has external linkage extern int i2 ; ............... reference to previous declaration which has internal linkage extern int i3 ; ............... reference to previous declaration which has external linkage extern int i4 ; ............... reference to previous declaration which has external linkage extern int i5 ; ............... reference to previous declaration which has internal linkage
user?s manual u15556ej1v0um 137 chapter 9 preprocessing directives (compiler directives) a preprocessing directive is a string of preprocessing tokens between the # preprocessing token and the line feed character. blank characters that can be used between preprocessing token strings are only spaces and horizontal tabs. a preprocessing directive specifies the processing performed before compiling a source file. preprocessing directives include operations such as processing or skipping a part of a source file depending on the conditions, obtaining additional code from other source files, and replacing the original source code with other text as in macro expansion. the preprocessing directives are described below. 9.1 conditional translation directives conditional translation skips part of a source file according to the value of a constant expression. if the value of the constant expression specified by a conditional translation directive is 0, the statements that follow the directive are not translated (compiled). the sizeof operator, cast operator, or an enumerated type constant cannot be used in the constant expression of any conditional translation directive. conditional translation directives include #if, #elif, #ifdef, #ifndef, #else, and #endif . in preprocessing directives, the following unary expressions called defined expressions may be used. defined identifier or defined (identifier) the unary expression returns 1 if the identifier has been defined with the #define preprocessing directive and 0 if the identifier has never been defined or its definition has been canceled. [example] in this example, the unary expression returns 1 and compiles between #if and #endif because sym has been defined (for the explanation of #if through #endif , refer to the explanations on the following pages). #define sym 0 #if defined sym : #endif
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 138 (1) #if directive conditional translation directives #if function the #if directive tells the translation phase of c to skip (discard) a section of source code if the value of the constant expression is 0. syntax #if constant expression line feed ? group ? example #if flag == 0 : #endif explanation in the above example, the constant expression ? flag == 0 ? is evaluated to determine whether a set of statements (i.e., source code) between #if and #endif is to be used in the translation phase. if the value of ? flag ? is nonzero, the source code between #if and #endif will be discarded. if the value is zero, the source code between #if and #endif will be translated.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 139 (2) #elif directive conditional translation directives #elif function the #elif directive normally follows the #if directive. if the value of the constant expression of the #if directive is 0, the constant expression of the #elif directive is evaluated. if the constant expression of the #elif directive is 0, the translation phase of c will skip (discard) the statements (a section of source code) between #elif and #endif . syntax #elif constant-expression line feed ? group ? example #if flag == 0 : #elif flag != 0 : #endif explanation in the above example, the constant expression ? flag= =0 ? or ? flag!=0 ? is evaluated to determine whether a set of statements that follow #if and another set of statements that follow #elif is to be used in the translation phase. if the value of ? flag ? is zero, the source code between #if and #elif will be translated. if the value is nonzero, the source code between #elif and #endif will be translated.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 140 (3) #ifdef directive conditional translation directives #ifdef function the #ifdef directive is equivalent to: #if defined (identifier) if the identifier has been defined with the #define directive, the statements between #ifdef and #endif will be translated. if the identifier has never been defined or its definition has been canceled, the translation phase will skip the source code between #ifdef and #endif . syntax #ifdef identifier line feed ? group ? example #define on #ifdef on : #endif explanation in the above example, the identifier ? on ? has been defined with the #define directive. thus, the source code between #ifdef and #endif will be translated. if the identifier ? on ? has never been defined, the source code between #ifdef and #endif will be discarded.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 141 (4) #ifndef directive conditional translation directives #ifndef function the #ifndef directive is equivalent to: #if !defined (identifier) if the identifier has never been defined with the #define directive, the source code between #ifndef and #endif will not be translated. syntax #ifndef identifier line feed ? group ? example #define on #ifndef on : #endif explanation in the above example, the identifier ? on ? has been defined with the #define directive. thus, the source code between #ifndef and #endif will be discarded in the translation phase. if the identifier ? on ? has never been defined, the source code between #ifndef and #endif will be translated.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 142 (5) #else directive conditional translation directives #else function the #else directive tells the translation phase of c to discard a section of source code that follows #else if the identifier of the preceding conditional translation directive is nonzero. the #if, #elif, #ifdef, or #ifndef directive may precede the #else directive. syntax #else line feed ? group ? example #define on #ifdef on : #else : #endif explanation in the above example, the identifier ? on ? has been defined with the #define directive. thus, the source code between #ifndef and #endif will be translated. if the identifier ? on ? has never been defined, the source code between #else and #endif will be translated.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 143 (6) #endif directive conditional translation directives #endif function the #endif directive indicates the end of a #ifdef block. syntax #endif line feed example #define on #ifdef on : : #endif explanation in the above example, #endif indicates the end of the #ifdef block (effective range of #ifdef directive).
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 144 9.2 source file inclusion directive the preprocessing directive #include searches for a specified header file and replaces #include by the entire contents of the specified file. the #include directive may take one of the following three forms for inclusion of other source files.  #include  #include ? file-name ?  #include preprocessing token string an #include directive may appear in the source obtained by #include . in this compiler, however, there are restrictions for #include directive nesting. for the restrictions, refer to table 1-1 maximum performance characteristics of this c compiler . remark preprocessing token string: character string defined by the #define directive
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 145 (1) #include < > source file inclusion directive #include< > function if the directive form is #include < >, the c compiler searches the directory specified by the -i compiler option, the directory specified by the inc78k environment variable, and the directory \nectools32\inc78k4 registered in the registry for the header file specified in angle brackets and replaces the #include directive line with the entire contents of the specified file. syntax #include line feed example #include explanation in the above example, the c compiler searches the directory specified by the inc78k environment variable and the directory \nectools32\inc78k4 registered in the registry for the file stdio.h and replaces the directive line #include with the entire contents of the specified file stdio.h . caution the above directories differ depending on the installation method.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 146 (2) #include ? ? source file inclusion directive #include ? ? function if the directive form is #include ? ? , the current working directory is first searched for the header file specified in double quotes. if it is not found, the directory specified by the -i compiler option, the directory specified by the inc78k environment variable, and the directory \nectools32\inc78k4 registered in the registry are searched. the compiler then replaces the #include directive line with the entire contents of the specified file. syntax #include ? file-name ? line feed example #include ?myprog. h? explanation in the above example, the c compiler searches the current working directory, the directory specified by the inc78k environment variable, and the directory \nectools32\inc78k4 registered in the registry for the file myprog.h specified in double quotes and replaces the directive line #include ?myprog.h? with the entire contents of the specified file myprog.h . caution the above directories differ depending on the installation method.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 147 (3) #include preprocessing token string source file inclusion directive #include token string function if the directive form is #include preprocessing token string, the header file to be searched is specified by macro replacement and the #include directive line is replaced by the entire contents of the specified file. syntax #include preprocessing token string line feed example #define incfile ?myprog.h? #include incfile explanation when including source files using the directive form ? #include preprocessing token string line feed ? , the specified ? preprocessing token string ? must be substituted with or ? file name ? by macro replacement. if the token string is replaced by , the c compiler searches the directory specified by the -i compiler option, the directory specified by the inc78k environment variable, and the directory \nectools32\inc78k4 registered in the registry for the specified file. if the token string is replaced by ? file name ? , the current working directory is searched. if the specified file is not found, the directory specified by the -i compiler option, the directory specified by the inc78k environment variable, and the directory \nectools32\inc78k4 registered in the registry are searched. caution the above directories differ depending on the installation method.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 148 9.3 macro replacement directives the macro replacement directives #define and #undef are used to replace the character string specified by ? identifier ? with ? substitution list ? and to end the scope of the identifier given by the #define , respectively. the #define directive has two forms: object format and function format.  object format #define identifier replacement list line feed  function format #define identifier ( ? identifier-list ? ) replacement-list line feed (1) actual argument replacement actual argument replacement is executed after the arguments in the function-form macro call are identified. if the # or ## preprocessing token is not prefixed to a parameter in the replacement list or if the ## preprocessing token does not follow any such parameter, all macros in the list will be expanded before replacement with the corresponding macro arguments. (2) # operator the # preprocessing token replaces the corresponding macro argument with a char string processing token. in other words, if this preprocessing token is prefixed to a parameter in the replacement list, the corresponding macro argument will be translated into a character or character string. (3) ## operator the ## preprocessing token concatenates the two tokens on either side of the ## symbol into one token. this concatenation will take place before the next macro expansion and the ## preprocessing token will be deleted after the concatenation. the token generated from this concatenation will undergo macro expansion if it has a macro name. [example of ## operation] the above macro replacement directive will be expanded as follows. printf(?x? ?1??=%d, x? ?2? ?=%s?, x1, x2); the concatenated char string will look like this. printf (?x1=%d, x2=%s?, x1, x2); #include #define debug(s, t) printf(?x?#s?= %d, x?#t?=%s?, x##s, x##t); void main() { int x1, x2; debug (1, 2); }
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 149 (4) re-scanning and further replacement the preprocessing token string resulting from replacement of macro parameters in the list will be scanned again, together with all remaining preprocessing tokens in the source file. macro names currently being replaced (not including the remaining preprocessing tokens in the source file) will not be replaced even if they are found during scanning of the replacement list. (5) scope of macro definition a macro definition ( #define directive) continues macro replacement until it encounters the corresponding #undef directive.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 150 (6) #define directive macro replacement directives #define function the #define directive in its simplest form replaces the specified identifier (manifest) with a given replacement list (any character sequence that does not contain a line feed) whenever the same identifier appears in the source code after the definition by this directive. syntax #define identifier replacement list line feed example #define pai 3.1415 explanation in the above example, the identifier ? pai ? will be replaced with ? 3.1415 ? whenever it appears in the source code after the definition by this directive.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 151 (7) #define( ) directive macro replacement directives #define ( ) function the function-form #define directive which has the form: #define name (name, ..., name) replacement list replaces the identifier specified in the function format with a given replacement list (any character sequence that does not contain a line feed). no white space is allowed between the first name and the opening parenthesis ? ( ? . this list of names (identifier list) may be empty. because this form of the directive defines a macro, the macro call will be replaced with the parameters of the macro inside the parentheses. syntax #define identifier ( ? identifier list ? ) replacement-list line feed example #define f(n) (n*n) void main() { int i; i=f(2) } explanation in the above example, #define directive will replace ? f(2) ? with ? (2*2) ? and thus the value of i will become 4. for the sake of safety, be sure to enclose the replacement list in parentheses, because unlike a function definition, this function-form macro is merely to replace a sequence of characters.
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 152 (8) #undef directive macro replacement directives #undef function the #undef directive undefines the given identifier. in other words, this directive ends the scope of the identifier that has been set by the corresponding #define directive. syntax #undef identifier line feed example #define f(n) (n*n) : #undef f explanation in the above example, #undef directive will invalidate the identifier ? f ? previously specified by ? #define f(n) (n*n) ? .
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 153 9.4 line control directive the preprocessing directive for line control #line replaces the line number to be used by the c compiler in translation with the number specified in this directive. if a string (character string) is given in addition to the number, the directive also replaces the source file name the c compiler has with the specified string. (1) to change the line number to change the line number, the specification is made as follows. 0 and numbers larger than 32767 cannot be specified. #line numeric-string line feed [example] #line 10 (2) to change the line number and the file name to change the line number and file name, the specification is made as follows. #line numeric-string ? character string ? line feed [example] #line 10 ?file1.c? (3) to change using preprocessing token string in addition to the specifications above, the following specification can also be made. in this case, the specified preprocessing token string must be either one of the above two examples after all the replacement. #line preprocessing-token-string line feed [example] #define line_num 100 #line line_num
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 154 9.5 #error preprocessing directive the #error preprocessing directive is a directive that outputs a message including the specified preprocessing tokens and incompletely terminates compileation. this preprocessing directive is used to terminate compilation. this preprocessing directive is specified as follows. #error ? preprocessing-token-string ? line feed [example] in this example, the macro name _ _k4 _ _ , which indicates the device series of this compiler, is used. if the device is the 78k/iv series, the program between #if and #else is compiled. in the other cases, the program between #else and #endif is compiled, but compilation will be terminated with an error message ? not for 78k4 ? output by the #error directive. #if _ _k4_ _ : #else #error ?not for 78k4? : #endif
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 155 9.6 #pragma directives #pragma directives are directives to instruct the compiler to operate using the compiler definition method. in this compiler, there are several #pragma directives to generate codes for the 78k/iv series (for details of #pragma directives, refer to chapter 11 extended functions) . [example] in this example, the #pragma nop directive enables the description to directly output a nop instruction in the c source. #pragma nop 9.7 null directives source lines that contain only the # character and white space are called null directives. null directives are simply discarded during preprocessing. in other words, these directives have no effect on the compiler. the syntax of null directives is given below. # line feed
chapter 9 preproc essing directives (compiler directives) user ? s manual u15556ej1v0um 156 9.8 compiler-defined macro names in this c compiler, the following macro names have been defined. _ _line_ _ line number of the current source line (decimal constant) _ _file_ _ source file name (string literal) _ _date_ _ date the source file was compiled (string literal in the form of ? mmm dd yyyy ? ) _ _time_ _ time of day the source file was compiled (string literal in the form of ? hh:mm:ss ? ) _ _stdc_ _ decimal constant ? 1 ? that indicates the compliance with ansi note specification note ansi is the acronym for american national standards institute a #define or #undef preprocessing directive must not be applied to these macro name and defined identifiers. all the macro names of the compiler definition start with an underscore followed by an uppercase character or a second underscore. in addition to the above macro names, macro names indicating the series name of devices according to the device subject to applied product development and macro names indicating device names are provided. to output the object code for the target device, these macro names must be specified by the option at compilation or by the processor type in the c source.  macro name indicating the series name of devices ? _ _k4 _ _ ?  macro name indicating the device name ? _ _ ? is added before the device type name and ? _ ? is added after the device type name. describe letters in uppercase (example) _ _4026_ _ _4038y_ remark the device type names are the same as those specified by the -c option. for the device type names, refer to the reference materials related to device files.
chapter 9 preproc essing directives (compiler directives) user?s manual u15556ej1v0um 157 this c compiler has a macro name indicating the memory model or location.  macro name indicating memory model when small model is specified #define _ _k4_small_ _ 1 when medium model is specified #define _ _k4_medium_ _ 1 when large model is specified #define _ _k4_large_ _ 1  macro name indicating location location 0 #define _ _k4loc0_ _ 1 location 15 #define _ _k4loc15_ _ 1 the device type for compilation is specified by adding the following to the command line ?-c device type name? example cc78k4 -c4038y prime.c it is possible to avoid specifying the device type at compilation by specifying it at the start of the c source program. ?#pragma pc (device type)? example #pragma pc (4038y) : however, the following can be described before ?#pragma pc (device type)?  comment statement  preprocessing directives that do not generate definition/reference of variables nor functions.
user?s manual u15556ej1v0um 158 chapter 10 library functions c has no instructions to transfer (input or output) data to and from external sources (peripheral devices and equipment). this is because of the c language designer's intent to hold the functions of c to a minimum. however, for actually developing a system, i/o operations are requisite. thus, c is provided with library functions to perform i/o operations. this c compiler is provided with library functions such as i/o, character/memory manipulation, program control, and mathematical functions. this chapter describes the library functions provided in this compiler.
chapter 10 library functions user ? s manual u15556ej1v0um 159 10.1 interface between functions to use a library function, the function must be called. calling a library function is carried out by a call instruction. the arguments and return value of a function are passed via a stack and a register, respectively. however, when the old function interface supporting option ( -zo ) is not specified, the first argument is, if possible, also passed via a register. for the -zo option, refer to chapter 5 compiler option in the cc78k4 c compiler operation user's manual (u15557e) . 10.1.1 arguments placing or removing arguments on or from the stack is performed by the caller (calling function). the callee (called function) only references the argument values. however, when the argument is passed via a register, the callee directly refers to the register and copies the value of the argument to another register, if necessary. also, when specifying the function call interface automatic pascal function option -zr , removal of arguments from the stack is performed by the called side if the argument is passed by the stack. arguments are placed on the stack one by one in descending order from bottom to top if the argument is passed via the stack. the minimum unit of data that can be stacked is 16 bits. a data type larger than 16 bits is stacked in units of 16 bits one by one from its msb. an 8-bit type data is extended to a 16-bit type data for stacking. when it is a large model and the argument is the address value or when it is a medium model and the argument is the address value of the function, the argument is stacked in 3-byte units. the following shows the list of the passing of the first argument. the second and subsequent arguments are passed via a stack. the function interface (passing of argument and storing of return value) of the standard library is the same as that of normal function. table 10-1. list of passing first argument type of first argument passing method (without -zo specification) passing method (with -zo specification) 1-byte, 2-byte integer ax passed via a stack 3-byte integer whl, small model: stack passing passed via a stack 4-byte integer ax, rp2 passed via a stack floating-point number (float type) ax, rp2 passed via a stack floating-point number (double type) ax, rp2 passed via a stack other passed via a stack passed via a stack remark of the types shown above, 1- to 4-byte integers include structures and unions.
chapter 10 library functions user?s manual u15556ej1v0um 160 10.1.2 return values the return value of a function is stored in units of 16 bits starting from its lsb in the direction from the register bc to the register rpz. when returning a structure, the first address of the structure is stored in the register bc. when returning a pointer, the first address of the structure is stored in the register bc. the following shows the list of storing the return value. the method of storing return values is the same as that of normal functions. table 10-2. list of storing return value return value type small model medium model large model 1 bit cy cy cy 1-byte, 2-byte integers bc bc bc 4-byte integers bc (lower), rp2 (higher) bc (lower), rp2 (higher) bc (lower), rp2 (higher) floating-point number (float type) bc (lower), rp2 (higher) bc (lower), rp2 (higher) bc (lower), rp2 (higher) floating-point number (double type) bc (lower), rp2 (higher) bc (lower), rp2 (higher) bc (lower), rp2 (higher) structure copies the structure to return to the area specific to the function and stores the address in bc copies the structure to return to the area specific to the function and stores the address in bc copies the structure to return to the area specific to the function and stores the address in tde pointer bc bc (function pointer) whl (function pointer) tde 10.1.3 saving registers to be used by individual libraries each library that uses rp3, rg4 (vvp) and rg5 (uup) saves the registers it uses to a stack. each library that uses a saddr area saves the saddr area it uses to a stack. a stack area is used as a work area for each library. (1) when -zr option is not specified the procedure of passing arguments and return values is shown below. an example of the small model is shown below. called function ?long func (int a, long b, char *c) ;?
chapter 10 library functions user?s manual u15556ej1v0um 161 <1> placing arguments on the stack (by the caller) the higher 16 bits of arguments ?c? and ?b? and lower 16 bits of argument ?b? are placed on the stack in the order named. a is passed via the ax register. <2> calling func by call instruction (by the caller) the return address is placed on the stack next to the lower 16 bits of argument ?b? and control is transferred to the function func . <3> saving registers to be used within the function (by the callee) if register rp3 is to be used, rp3 is placed on the stack. <4> placing the first argument passed via the register on the stack (by the callee) <5> processing func and storing the return value in registers (by the callee) the lower 16 bits of the return value ?long? are stored in bc and the higher 16 bits of the return value, are stored are stored in rp2. <6> restoring the stored first argument (by the callee) <7> restoring the saved registers (by the callee) <8> returning control to the caller with ret instruction (by the callee) <9> removing arguments from the stack (by the caller) the number of bytes (in units of 2 bytes) of the arguments is added to the stack pointer. in the example shown in figure 10-1, 6 is added. figure 10-1. stack area when function is called (no ?zr specified) stack pointer after < 6 > stack pointer after < 7 > stack pointer after < 8 > stack pointer after < 9 > stack pointer after < 4 > stack pointer after < 3 > stack pointer after < 2 > stack pointer after < 1 > stack pointer before stacking arguments bc rp2 lower 16 bits higher 16 bits return value in < 5 > is stored a rp3 return address lower 16 bits of b higher 16 bits of b c high address
chapter 10 library functions user ? s manual u15556ej1v0um 162 (2) when -zr option is specified the following example shows the procedure of passing arguments and return values when the -zr option is specified. called function ?long func (int a, long b, char *c);? <1> place arguments on the stack (by the caller) the higher 16 bits of arguments ? c ? and ? b ? and the lower 16 bits of argument ? b ? are placed on the stack in the order named. a is passed via the ax register. <2> call func by call instruction (by the caller). the return address is placed on the stack next to the lower 16 bits of argument ? b ? and control is transferred to the function func . <3> save the registers used in the functions (by the caller). <4> perform processing of the function func , and store return values in the register (by the callee). store the lower 16 bits of the return value (long) in bc and the higher 16 bits in rp2. <5> restore the saved registers (by the callee). <6> save the return address in the register (by the callee). save the return address in the whl register. <7> the caller restores the placed arguments (by the callee). <8> return control to the function on the caller in the branch instruction (by the callee) at the value saved in the register in <6>. return control to the function on the caller in the br whl instruction (by the callee). figure 10-2. stack area when function is called ( ? zr specified) stack pointer after < 5 > stack pointer after < 6 > stack pointer after < 7 > stack pointer after < 2 > stack pointer after < 1 > stack pointer after < 3 > stack pointer before stacking arguments bc rp2 lower 16 bits higher 16 bits return value in < 4 > is stored rp3 return address lower 16 bits of b higher 16 bits of b c high address
chapter 10 library functions user ? s manual u15556ej1v0um 163 10.2 headers this c compiler has 13 headers (or header files). each header defines or declares standard library functions, data type names, and macro names. these 13 headers are as shown below. ctype.h setjmp.h stdarg.h stdio.h stdlib.h string.h error.h errno.h limits.h stddef.h math.h float.h assert.h (1) ctype.h this header is used to define character and string functions. in this standard header, the following library functions have been defined. however, when the compiler option -za (the option that disables the functions not complying with ansi specifications and enables a part of the functions of ansi specifications) is specified, _toupper and _tolower are not defined. instead, tolow and toup are defined. when -za is not specified, tolow and toup are not defined. isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper isascii toascii _toupper _tolower tolow toup (2) setjmp.h this header is used to define program control functions. in this standard header, the setjmp and longjmp functions have been defined. in the header setjmp.h , the following object has been defined. [declaration of char array type jmp_buf with an array size of greater than 30] typedef char jmp_buf[30] (3) stdarg.h this header used to define special functions. in this standard header, the following four library functions have been defined. when the -zo option (old function interface supporting option) is not specified, the va_start function cannot be specified for the first argument because the first argument is passed via the register. use the macro in the following manner when the -zo option is not specified. ? use the va_starttop macro when specifying the first argument. ? use the va_start macro when specifying the second argument. va_start va_starttop va_arg va_end in the header stdarg.h the following object has been declared.
chapter 10 library functions user ? s manual u15556ej1v0um 164 [declaration of pointer type va_list to char ] typedef char *va_list; (4) stdio.h this header is used to define i/o functions. in this standard header, the following functions have been defined. sprintf sscanf printf scanf vprintf vsprintf getchar gets putchar puts the following macro names are declared. #define eof (?1) #define null (void *)0 (5) stdlib.h this header is used to define character and string functions, memory functions, program control functions, mathematical functions, and special functions. in this standard header, the following library functions have been defined. however, when the compiler option -za (the option that disables the functions not complying with ansi specifications and enables a part of the functions of ansi specifications) is specified, brk , sbrk , itoa , ltoa , and ultoa are not defined. instead, strbrk , strsbrk , stritoa , strltoa , and strultoa are defined. when -za is not specified, strbrk , strsbrk , stritoa , strltoa , and strultoa are not defined. atoi atol strtol strtoul calloc free malloc realloc abort atexit exit abs div labs ldiv brk sbrk atof strtod itoa ltoa ultoa rand srand bsearch qsort strbrk strsbrk stritoa strltoa strultoa in the header stdlib.h the following objects have been defined. [declaration of structure type ? div_t ? which has int type members ? quot ? and ? rem ? ] typedef struct { int quot ; int rem ; } div_t ; [declaration of structure type ldiv_t which has long int type members quot and rem ] typedef struct { long int quot ; long int rem ; } ldiv_t ; [definition of macro name rand_max ] #define rand_max 32767
chapter 10 library functions user ? s manual u15556ej1v0um 165 [declaration of macro name] define exit_success 0 define exit_failure 1 (6) string.h this header is used to define character and string functions, memory functions, and special functions. in this standard header, the following library functions have been defined. memcpy memmove strcpy stmcpy strcat strncat memcmp strcmp strncmp memchr strchr strcspn strpbrk strrchr strspn strstr strtok memset strerror strlen strcoll strxfrm (7) error.h error.h includes errno.h . (8) errno.h in this standard header, the following objects have been defined. [definitions of macro names ? edom ? , ? erange ? , and ? enomem ? ] #define edom 1 #define erange 2 #define enomem 3 [declaration of volatile int type external variable errno ] extern volatile int errno ; (9) limits.h in this standard header, the following macro names have been defined. #define char_bit 8 #define char_max +127 #define char_min ?128 #define int_max +32767 #define int_min ?32768 #define long_max +2147483647 #define long_min ?2147483648 #define schar_max +127 #define schar_min ?128 #define shrt_max +32767 #define shrt_min ?32768 #define uchar_max 255u #define uint_max 65535u #define ulong_max 4294967295u #define ushrt_max 65535u
chapter 10 library functions user ? s manual u15556ej1v0um 166 however, when the -qu option, which regards unqualified char as unsigned char, is specified, char_max and char_min are declared by the macro _char_unsigned_ _ declared by the compiler as follows. #define char_max (255u) #define char_min (0) (10) stddef.h in this standard header, the following objects have been declared and defined. [declaration of int type ptrdiff_t ] typedef int ptrdiff_t; [declaration of unsigned int type size_t ] typedef unsigned int size_t; [definition of macro name null ] #define null (void*)0 [definition of macro name offsetof ] #define offsetof (type, member) ((size_t)&(((type*)0) -> member))  offsetof (type, member specifier) offsetof is expanded to a general integer constant expression with the type size_t, and the value is an offset value in byte units from the start of the structure (that is specified by the type) to the structure member (that is specified by the member specifier). the member specifier must be the one that the result of evaluation of the expression & (t. member specifier) becomes an address constant when static type t; is declared. when the specified member is a bit field, the operation will not be guaranteed. (11) math.h math.h defines the following functions. acos asin atan atan2 cos sin tan cosh sinh tqnh exp frexp ldexp log log10 modif pow sqrt ceil fabs floor fmod acosf asinf atanf atan21 cost sinf tanf coshf sinhf tanhf expf frexpf ldexpf logf log10f modff powf sqrtf ceilf fabsf floorf fmodf matherr the following objects are defined. [definition of macro name huge_val ] #define huge_val _huge
chapter 10 library functions user?s manual u15556ej1v0um 167 (12) float.h float.h defines the following objects. when the size of a double type is 32 bits, the macros to be defined are sorted by the macro _ _double_is_32bits_ _ declared by the compiler. #ifndef _float_h #define flt_rounds 1 #define flt_radix 2 #ifdef _ _double_is_32bits _ _ #define flt_mant_dig 24 #define dbl_mant_dig 24 #define ldbl_mant_dig 24 #define flt_dig 6 #define dbl_dig 6 #define ldbl_dig 6 #define flt_min_exp ?125 #define dbl_min_exp ?125 #define ldbl_min_exp ?125 #define flt_min_10_exp ?37 #define dbl_min_10_exp ?37 #define ldbl_min_10_exp ?37 #define flt_max_exp +128 #define dbl_max_exp +128 #define ldbl_max_exp +128 #define flt=max=10=exp +38 #define dbl_max_10_exp +38 #define ldbl_max_10_exp +38 #define flt_max 3.40282347e+38f #define dbl_max 3.40282347e+38f #define ldbl_max 3.40282347e+38f #define flt_epsilon 1.19209290e?07f #define dbl_epsilon 1.19209290e?07f #define ldbl_epsilon 1.19209290e?07f #define flt_min 1.1749435e?38f #define dbl_min 1.17549435e?38f #define ldbl_min 1.17549435e?38f
chapter 10 library functions user?s manual u15556ej1v0um 168 #else /* _ _double_is_32bits_ _ */ #define flt_mant_dig 24 #define dbl_mant_dig 53 #define ldbl_mant_dig 53 #define flt_dig 6 #define dbl_dig 15 #define ldbl_dig 15 #define flt_min_exp ?125 #define dbl_min_exp ?1021 #define ldbl_min_exp ?1021 #define flt_min_10_exp ?37 #define dbl_min_10_exp ?307 #define ldbl_min_10_exp ?307 #define flt_max_exp +128 #define dbl_max_exp +1024 #define ldbl_max_exp +1024 #define flt_max_10_exp +38 #define dbl_max_10_exp +308 #define ldbl_max_10_exp +308 #define flt_max 3.40282347e+38f #define dbl_max 1.7976931348623157e+308 #define ldbl_max 1.7976931348623157e+308 #define flt_epsilon 1.19209290e-07f #define dbl_epsilon 2.2204460492503131e-016 #define ldbl_epsilon 2.2204460492503131e-016 #define flt_min 1.17549435e-38f #define dbl_min 2.225073858507201e-308 #define ldbl_min 2.225073858507201e-308 #endif /* _ _double_is_32bits_ _ */ #define _float_h #endif /* !_float_h */
chapter 10 library functions user?s manual u15556ej1v0um 169 (13) assert.h assert.h defines the following objects. #ifdef ndebug #define assert (p) ((void)0) #else extern int _ _assertfail (char*_ _msg, char*_ _cond, char*_ _file, int_ _line); #define assert (p) ((p) ? (void) 0 : (void)_ _assertfail ?assertion failed: %s, file %s, line %d\n?, #p, _ _file_ _, _ _line_ _)) #endif /* ndebug */ however, the assert.h header file is not defined in the assert.h header file. if the assert.h header file references another macro, ndebug, which is not defined by the assert.h header file, and if ndebug is defined as a macro when assert.h is captured to the source file, the assert.h header file simply declares the assert macro as: #define assert(p) ((void)0) and does not define _ _ assertfail . 10.3 re-entrantability re-entrant is a state where a function called from a program can be consecutively called from another program. the standard library of this compiler does not use static area allowing re-entrantability. therefore, data in the storage used by functions will not be destroyed by a call from another program. however, the functions shown in (1) to (3) are not re-entrant. (1) functions that cannot be re-entranced setjmp , longjmp , atexit , exit (2) functions that use the area secured in the startup routine div , ldiv , brk , sbrk , rand , srand , strtok (3) functions that deal with floating-point numbers sprintf , sscanf , printf , scanf , vprintf , vsprintf note , atof , strtod , and all the mathematical functions note among sprintf , sscanf , printf , scanf , vprintf , and vsprintf , functions that do not support floating- point numbers are re-entrant.
chapter 10 library functions user?s manual u15556ej1v0um 170 10.4 standard library functions this section explains the standard library functions of this c compiler classified by function as follows. all standard library functions are supported even when the ?zf option is specified.  item (1-x) character and character string functions  item (2-x) program control functions  item (3-x) special functions  item (4-x) i/o functions  item (5-x) utility functions  item (6-x) character string/memory functions  item (7-x) mathematical functions  item (8-x) diagnostic functions
chapter 10 library functions user?s manual u15556ej1v0um 171 1-1 is character & string functions function is judges the type of character. header ctype.h for all the character functions function prototype int is (int c); function arguments return value is c... character to be judged 1 if character c is included in the character range. 0 if character c is not included in the character range. explanation function character range isalpha alphabetic character a to z or a to z isupper uppercase letters a to z islower lowercase letters a to z isdigit numeric characters 0 to 9 isalnum alphanumeric characters 0 to 9 and a to z or a to z isxdigit hexadecimal numbers 0 to 9 and a to f or a to f isspace white-space characters (space, tab, carriage return, line feed, vertical tab, and form feed) ispunct punctuation characters except white-space characters isprint printable characters isgraph printable nonblank characters iscntrl control characters isascii ascii character set
chapter 10 library functions user?s manual u15556ej1v0um 172 1-2 toupper, character & string functions tolower function the character functions toupper and tolower both convert one type of character to another. the toupper function returns the uppercase equivalent of c if c is a lowercase letter. the tolower function returns the lowercase equivalent of c if c is a uppercase letter. header ctype.h function prototype int to (int c); function arguments return value toupper, tolower c character to be converted uppercase equivalent if c is a convertible character. character ?c? is returned unchanged if not convertible. explanation toupper  the toupper function checks to see if the argument is a lowercase letter and if so converts the letter to its uppercase equivalent. tolower  the tolower function checks to see if the argument is a uppercase letter and if so converts the letter to its lowercase equivalent.
chapter 10 library functions user?s manual u15556ej1v0um 173 1-3 toascii character & string functions function the character function toascii converts ?c? to an ascii code. header ctype.h function prototype int toascii (int c); function arguments return value toascii c... character to be converted value obtained by converting the bits outside the ascii code range of ?c? to 0. explanation the toascii function converts the bits (bits 7 to 15) of ?c? outside the ascii code range of ?c? (bits 0 to 6) to ?0? and returns the converted bit value.
chapter 10 library functions user?s manual u15556ej1v0um 174 1-4 _toupper/toup character & string functions _tolower/tolow function the character function _ toupper / toup subtracts ?a? from ?c? and adds ?a? to the result. the character function _ tolower / tolow subtracts ?a? from ?c? and adds ?a? to the result. ( _toupper is exactly the same as toup, and _tolower is exactly the same as tolow ) remark a: lowercase, a: uppercase header ctype.h function prototype int _to (int c); function arguments return value _toupper toup c... character to be converted value obtained by adding ?a? to the result of subtraction ?c? - ?a? _tolower tolow value obtained by adding ?a? to the result of subtraction ?c? - ?a? remark a: lowercase, a: uppercase explanation _ toupper  the _ toupper function is similar to toupper except that it does not test to see if the argument is a lowercase letter. _ tolower  the _ tolower function is similar to tolower , except it does not test to see if the argument is an uppercase letter.
chapter 10 library functions user?s manual u15556ej1v0um 175 2-1 setjmp, program control functions longjmp function the program control function setjmp saves the environment information when a call to this function is made. the program control function longjmp restores the environment information saved by setjmp . header setjmp. h function prototype int setjmp (jmp_buf env); void longjmp (jmp_buf env, int val); ( jmp_buf is typedef defined with setjmp.h .) function arguments return value setjmp env ... array to which environment information is to be saved  0 if called directly  value given by ?val? if returning from the corresponding longjmp or 1 if ?val ? is 0 longjmp env ... array to which environment information was saved by setjmp val ... return value to setjmp longjmp will not return because program execution resumes at statement next to setjmp that saved environment to ?env?. explanation setjmp  the setjmp function saves the rp3, rg4, rg5 registers, saddr area and sp to be used as variable registers, and the return address of the functions to the array (or information block) referred to as env and returns 0. longjmp  the longjmp function restores the environment information (rp3, rg4, rg5 registers, saddr area and sp to be used as variable registers) saved to env . program execution continues as if the value given by val (or 1 if the value of val is 0) was returned by the corresponding setjmp .
chapter 10 library functions user?s manual u15556ej1v0um 176 3-1 va_start, special functions va_starttop, va_arg, va_end function the va_start function (macro) is used to start a variable argument list. the va_starttop function (macro) is used to start a variable argument list. the va_arg function (macro) obtains the value of an argument from a variable argument list. the va_end function (macro) indicates that the end of a variable argument list is reached. header stdarg. h function prototype void va_start (va_list ap, parmn); void va_starttop(va_list ap,parmn); type va_arg (va_list ap, type); void va_end (va_list ap); va-list is typedef defined with stdarg.h. function arguments return value va_start va_starttop va_list ..... variable argument list ap ... variable to be initialized so that it can be used in va_arg and va_end parmn ... name of last parameter in function prototype (one immediately proceeding ellipsis ?...?) none va_arg va_list ap ... variable argument list. ap must be set up with call to va_start before calling va_arg type... type of argument to be obtained next value from argument list; 0 if ap is a null pointer va_end va_list ap .... variable argument list. ap must be set up with call to va_start before calling va_arg . none
chapter 10 library functions user?s manual u15556ej1v0um 177 va_start, special functions va_starttop, va_arg, va_end explanation va_start  in the va_start macro, the argument ap (argument pointer) must be a va_list type ( char * type) object.  a pointer to the next argument of parmn is stored in ap .  parmn is the name of the last (rightmost) parameter specified in the function's prototype.  if parmn has the register storage class, proper operation of this function is not guaranteed.  if parmn is the first argument, proper operation of this function is not guaranteed. va_starttop ? when the -zo option (old function interface supporting option) is not specified, the va_start function cannot be specified for the first argument because the first argument is passed via the register. use the macro in the following manner when the -zo option is not specified. ? use the va_starttop macro when specifying the first argument. ? use the va_start macro when specifying the second argument. va_arg  in the va_arg macro, the argument ap must be the same as the va_list type object initialized with va_start .  after the argument pointer ap has been initialized via a call to va_start , parameters are returned via calls to va_arg , with type being the type of the next parameter. (each call to va_arg obtains the next value from the argument list.)  if the argument pointer ap is a null pointer, 0 (of type type) is returned. va_end  the va_end macro sets a null pointer in the argument pointer ap to inform the macro processor that all the parameters in the variable argument list have been processed.
chapter 10 library functions user?s manual u15556ej1v0um 178 4-1 sprintf i/o functions function sprintf writes data into a character string according to the format. header stdio.h function prototype int sprintf (char *s,const char *format,...); function arguments return value sprintf s ... pointer to the string into which the output is to be written format ... pointer to the string that indicates format commands ... ... zero or more arguments to be converted number of characters written in s (terminating null character is not counted.) explanation  if there are fewer actual arguments than the formats, the proper operation is not guaranteed. if the formats run out despite the fact that actual arguments still remain, the excess actual arguments are only evaluated and ignored.  sprintf converts zero or more arguments that follow format according to the format command specified by format and writes (copies) them into the string s .  zero or more format commands may be used. ordinary characters (other than format commands that begin with a % character) are output as is to the string s . each format command takes zero or more arguments that follow format and outputs them to the string s .  each format command begins with a % character and is followed by these:  zero or more flags (to be explained later) that modify the meaning of the format command  optional decimal integer that specifies a minimum field width if the output width after the conversion is less than this minimum field width, this specifier pads the output with spaces or zeros on its left. (if the left-justifying flag ? ? ? (minus) sign follows %, zeros are padded out to the right of the output.) the default padding is done with spaces. if the output is to be padded with 0s, place a 0 before the field width specifier. if the number or string is greater than the minimum field width, it will be printed in full even if the minimum is exceeded.  optional precision (number of decimal places) specification (. integer) with d, i, o, u, x, and x type specifiers, the minimum number of digits is specified. with the s type specifier, the maximum number of characters (maximum field width) is specified. the number of digits to be output following the decimal point is specified for e, e, and f conversions. the number of maximum effective digits is specified for g and g conversions. this precision specification must be made in the form of (.integers). if the integer part is omitted, 0 is assumed to have been specified. the amount of padding resulting from this precision specification takes precedence over the padding by the field width specification.
chapter 10 library functions user?s manual u15556ej1v0um 179 sprintf i/o functions  optional h , l and l modifiers the h modifier instructs the sprintf function to perform the d, i, o, u, x, or x type conversion that follows this modifier on short int or unsigned short int type. the h modifier instructs the sprintf function to perform the n type conversion that follows this modifier on a pointer to short int type. the l modifier instructs the sprintf function to perform the d, i, o, u, x, or x type conversion that follows this modifier on long int or unsigned long int type. the h modifier instructs the sprintf function to perform the n type conversion that follows this modifier on a pointer to long int type. for other type specifiers, the h , l or l modifier is ignored.  character that specifies the conversion (to be explained later) in the minimum field width or precision (number of decimal places) specification, * may be used in place of an integer string. in this case, the integer value will be given by the int argument (before the argument to be converted). any negative field width resulting from this will be interpreted as a positive field that follows the ? (minus) flag. all negative precision will be ignored. the following flags are used to modify a format command. ? ................. the result of a conversion is left-justified within the field. + ................. the result of a signed conversion always begins with a + or ? sign. space.......... if the result of a signed conversion has no sign, a space is prefixed to the output. if the + (plus) flag and space flag are specified at the same time, the space flag will be ignored. # ................. the result is converted in the assignment form. in the o type conversion, precision is increased so that the first digit becomes 0. in the x or x type conversion, 0x or 0x is prefixed to a nonzero result. in the e, e, and f type conversions, a decimal point is forcibly inserted to all the output values (in the default without #, a decimal point is displayed only when there is a value to follow). in the g and g type conversions, a decimal point is forcibly inserted to all the output values, and truncation of 0 to follow will not be allowed (in the default without #, a decimal point is displayed only when there is a value to follow. the 0 to follow will be truncated). in all the other conversions, the # flag is ignored. the format codes for output conversion specifications are as follows. d ................. converts int argument to signed decimal format. i .................. converts int argument to signed decimal format. o ................. converts int argument to unsigned octal format. u ................. converts int argument to unsigned decimal format. x ................. converts int argument to unsigned hexadecimal format (with lowercase letters abcdef). x ................. converts int argument to unsigned hexadecimal format (with uppercase letters abcdef). with d , i , o , u , x and x type specifiers, the minimum number of digits (minimum field width) of the result is specified. if the output is shorter than the minimum field width, it is padded with zeros. if no precision is specified, 1 is assumed to have been specified. nothing will appear if 0 is converted with 0 precision.
chapter 10 library functions user?s manual u15556ej1v0um 180 sprintf i/o functions f .................. converts double argument as a signed value with [ ? ] dddd.dddd format. dddd is one or more decimal number(s). the number of digits before the decimal point is determined by the absolute value of the number, and the number of digits after the decimal point is determined by the required precision. when the precision is omitted, it is interpreted as 6. e ................. converts double argument as a signed value with [ ? ] d.dddd e [sign] ddd format. d is one decimal number, and dddd is one or more decimal number(s). ddd is exactly a three-digit decimal number, and the sign is + or ? . when the precision is omitted, it is interpreted as 6 e ................. the same format as that of e except e is added instead of e before the exponent. g ................. uses whichever shorter method of f or e format when converting double argument based on the specified precision. e format is used only when the exponent of the value is smaller than ? 4 or larger than the specified number by precision. the following 0 are truncated, and the decimal point is displayed only when one or more numbers follow. g ................. the same format as that of g except e is added instead of e before the exponent. c ................. converts int argument to unsigned char and writes the result as a single character. s ................. the associated argument is a pointer to a string of characters and the characters in the string are written up to the terminating null character (but not included in the output). if precision is specified, the characters exceeding the maximum field width will be truncated off the end. when the precision is not specified or larger than the array, the array must include a null character. p ................. the associated argument is a pointer to void and the pointer value is displayed in unsigned hexadecimal 4 digits (with 0s prefixed to less than a 4-digit pointer value). in the case of the large model, the pointer value is displayed in unsigned hexadecimal 8 digits (the higher 2 digits are padded by 0 and displayed with 0s prefixed to less than a 6-digit pointer value). the precision specification if any will be ignored. n ................. the associated argument is an integer pointer into which the number of characters written thus far in the string ?s? is placed. no conversion is performed. % ................ prints a % sign. the associated argument is not converted (but the flag and minimum field width specifications are effective).  operations for invalid conversion specifiers are not guaranteed.  when the actual argument is a union or a structure, or the pointer to indicate them (except the character type array in % s conversion or the pointer in % p conversion), operations are not guaranteed.  the conversion result will not be truncated even when there is no field width or the field width is small. in other words, when the number of characters of the conversion result are larger than the field width, the field is extended to the width that includes the conversion result.  the formats of the special output character string in %f, %e, %e, %g, %g conversions are shown below. non-numeric ?(nan)? + ?(+inf)? ? ?(?inf)? sprintf writes a null character at the end of the string s . (this character is included in the return value count.) the syntax of format commands is illustrated in figure 10-3.
chapter 10 library functions user?s manual u15556ej1v0um 181 figure 10-3. syntax of format commands format command ordinary char. format: characters except % ordinary characters: flags l format command: % min. field width precision h format code ? + space # flags: digits precision: * digits minimum field width: * format codes: d i o i u x x c s p n f e e g g % .
chapter 10 library functions user ? s manual u15556ej1v0um 182 4-2 sscanf i/o functions function sscanf reads data from the input string according to the format. header stdio.h function prototype int sscanf (const char *s, const char *format, ... ); function arguments return value sscanf s ... pointer to the input string format ... pointer to the string that indicates the input format commands ... ... pointer to object in which converted values are to be stored, and zero or more arguments ? 1 if the string s is empty. number of assigned input data items if the string s is not empty. explanation  sscanf inputs data from the string pointed to by s . the string pointed to by format specifies the input string allowed for input. zero or more arguments after format are used as pointers to an object. format specifies how data is to be converted from the input string.  if there are insufficient arguments to match the format commands pointed to by format , proper operation by the compiler is not guaranteed. for excessive arguments, expression evaluation will be performed but no data will be input.  the control string pointed to by format consists of zero or more format commands classified into the following three types. (1) white-space characters (one or more characters for which isspace becomes true) (2) non-white-space characters (other than %) (3) format specifiers  each format specifier begins with the % character and is followed by these:  optional * character which suppresses assignment of data to the corresponding argument  optional decimal integer which specifies a maximum field width  optional h , l or l modifier which indicates the object size on the receiving side if h precedes the d, i, o, or x format specifier, the argument is a pointer to not int but short int . if l precedes any of these format specifiers, the argument is a pointer to long int . likewise, if h precedes the u format specifier, the argument is a pointer to unsigned short int . if l precedes the u format specifier, the argument is a pointer to unsigned long int .  if l precedes the conversion specifier e , e , f , g , g , the argument is a pointer to double (a pointer to float in default without l ). if l precedes, it is ignored. remark conversion specifier: character to indicate the type of corresponding conversion (to be described later)
chapter 10 library functions user ? s manual u15556ej1v0um 183 sscanf i/o functions  sscanf executes the format commands in ? format ? in sequence and if any format command fails, the function will terminate. (1) a white-space character in the control string causes sscanf to read any number (including zero) of white-space characters up to the first non-white-space character (which will not be read). this white- space character command fails if it does not encounter any non-white-space characters. (2) a non-white-space character causes sscanf to read and discard a matching character. this command fails if the specified character is not found. (3) the format commands define a collection of input streams for each type specifier (to be detailed later). the format commands are executed according to the following steps.  the input white-space characters (specified by isspace ) are skipped over, except when the type specifier is [, c, or n .  the input data items are read from the string ? s ? , except when the type specifier is n . the input data items are defined as the longest input stream of the first partial stream of the string indicated by the type specifier (but up to the maximum field width if so specified). the character next to the input data items is interpreted as not have been read. if the length of the input data items is 0, the format command execution fails.  the input data items (number of input characters with the type specifier n ) are converted to the type specified by the type specifier except the type specifier %. if the input data items do not match the specified type, the command execution fails. unless assignment is suppressed by *, the result of the conversion is stored in the object pointed to by the first argument that follows ? format ? and has not yet received the result of the conversion.  the following type specifiers are available. d ....................... reads a decimal integer (which may be signed). the corresponding argument must be a pointer to an integer. i ........................ reads an integer (which may be signed). if a number is preceded by 0x or 0x, the number is interpreted as a hexadecimal integer. if a number is preceded by 0, the number is interpreted as an octal integer. other numbers are regarded as decimal integers. the corresponding argument must be a pointer to an integer. o ....................... reads an octal integer (which may be signed). the corresponding argument must be a pointer to an integer. u ....................... reads an unsigned decimal integer. the corresponding argument must be a pointer to an unsigned integer. x ....................... reads a hexadecimal integer (which may be signed). e , e , f , g , g ...... a floating-point value consists of an optional sign (+ or ? ), one or more consecutive decimal number(s) including a decimal point, an optional exponent ( e or e ), and the following optional signed integer value. when overflow occurs as a result of conversion, or when underflow occurs with the conversion result , a non-normalized number or 0 becomes the conversion result. the corresponding argument is a pointer to float . the corresponding argument must be a pointer to the first character of an array that has sufficient size to accommodate this character string and a null terminator. the null terminator will be automatically added.
chapter 10 library functions user ? s manual u15556ej1v0um 184 sscanf i/o functions s ................. inputs a character string consisting of a non-blank character string. the corresponding argument is a pointer to an integer. 0x or 0x can be allocated at the first hexadecimal integer. the corresponding argument must be a pointer an array that has sufficient size to accommodate this character string and a null terminator. the null terminator will be automatically added. [ .................. inputs a character string consisting of expected character groups (called a scanset ). the corresponding argument must be a pointer to the first character of an array that has sufficient size to accommodate this character string and a null terminator. the null terminator will be automatically added. the format commands continue from this character up to the closing square bracket (]). the character string (called a scanlist ) enclosed in the square brackets constitutes a scanset except when the character immediately after the opening square bracket is a circumflex (^). when the character is a circumflex, all the characters other than a scanlist between the circumflex and the closing square bracket constitute a scanset . however, when a scanlist begins with [ ] or [^], this closing square bracket is contained in the scanlist and the next closing square brocket becomes the end of the scanlist . a hyphen ( ? ) at other than the left or right end of a scanlist is interpreted as the punctuation mark for hyphenation if the character at the left of the range specifying hyphen ( ? ) is not smaller than the right-hand character in ascii code value. c ................. inputs a character string consisting of the number of characters specified by the field width. (if the field width specification is omitted, 1 is assumed.) the corresponding argument must be a pointer to the first character of an array that has sufficient size to accommodate this character string. the null terminator will not be added. p ................. reads an unsigned hexadecimal integer. the corresponding argument must be a pointer to void pointer. for the large model, a hexadecimal 8-digit integer is input, and the higher two digits are ignored. n ................. receives no input from the string s . the corresponding argument must be a pointer to an integer. the number of characters that are read thus far by this function from the string ? s ? is stored in the object that is pointed to by this pointer. the %n format command is not included in the return value assignment count. % ................ reads a % sign. neither conversion nor assignment takes place. if a format specification is invalid, the format command execution fails. if a null terminator appears in the input stream, sscanf will terminate. if an overflow occurs in an integer conversion (with the d, i, o, u, x, or p format specifier), the higher bits will be truncated depending on the number of bits of the data type after the conversion. the syntax of input format commands is illustrated below.
chapter 10 library functions user ? s manual u15556ej1v0um 185 figure 10-4. syntax of input format commands command format: characters except % and white space ordinary characters: digits max. field width: \f \n \r \t \v space white-space char. ordinary char. format specifier command: l format command: % max. field width h format specifier * i format specifiers: d i o u x s c p n % f e e g g scanlist scanlist: characters except characters except ^ white-space characters:
chapter 10 library functions user ? s manual u15556ej1v0um 186 4-3 printf i/o functions function printf outputs data to sfr according to the format. header stdio.h function prototype int printf (const char *format, ...); function arguments return value printf format ...pointer to the character string that indicates the output conversion specification ... ... 0 or more arguments to be converted number of characters output to s (the null character at the end is not counted) explanation ? (0 or more) arguments following the format are converted and output using the putchar function, according to the output conversion specification specified in the format. ? the output conversion specification is 0 or more directives. normal characters (other than conversion specifications starting with %) are output as is using the putchar function. the conversion specification is output using the putchar function by fetching and converting the following (0 or more) arguments. ? each conversion specification is the same as that of the sprintf function.
chapter 10 library functions user ? s manual u15556ej1v0um 187 4-4 scanf i/o functions function scanf reads data from sfr according to the format. header stdio.h function prototype int scanf (const char *format, ...); function arguments return value scanf format ... pointer to the character string to indicate input conversion specification format ... ... pointer (0 or more) argument to the object to assign the converted value when the character string s is not null ... number of input items assigned explanation  performs input using the getchar function. specifies the input string permitted by the character string indicated by format . uses the arguments after format as pointers to an object. format specifies how the conversion is performed by the input string.  when there are not enough arguments for format , normal operation is not guaranteed. when the number of arguments is excessive, the expression will be evaluated but not input.  format consists of 0 or more directives. the directives are as follows. (1) one or more null character (character that makes isspace true) (2) normal character (other than %) (3) conversion indication  if a conversion ends with an input character that conflicts with the directive, the conflicting input character is rounded down. the conversion indication is the same as that of the sscanf function.
chapter 10 library functions user ? s manual u15556ej1v0um 188 4-5 vprintf i/o functions function vprintf outputs data to sfr according to the format. header stdio.h function prototype int vprintf (const char *format, va_list p) ; function arguments return value vprintf format ... pointer to the character string that indicates output conversion specification p ... pointer to the argument list number of output characters (the null character at the end is not counted) explanation  the argument that the pointer of the argument list indicates is converted and output using the putchar function according to the output conversion specification specified by the format.  each conversion specification is the same as that of the sprintf function.
chapter 10 library functions user ? s manual u15556ej1v0um 189 4-6 vsprintf i/o functions function vsprintf writes data to character strings according to the format. header stdio.h function prototype int vsprintf (char *s, const char * format, va_list p) ; function arguments return value vsprintf s ... pointer to the character string that writes the output format ... pointer to the character string that indicates output conversion specification p ... pointer to the argument list number of characters output to s (the null character at the end is not counted) explanation  writes out the argument that the pointer of argument list indicates to the character strings indicated by s according to the output conversion specification specified by format .  the output specification is the same as that of the sprintf function.
chapter 10 library functions user ? s manual u15556ej1v0um 190 4-7 getchar i/o functions function getchar reads a character from sfr . header stdio.h. function prototype int getchar (void); function arguments return value getchar none a character read from sfr explanation  returns the value read from sfr symbol p0 (port 0).  an error check related to reading is not performed.  to change the sfr to be read, it is necessary to either change the source and re-register it to the library or create a new getchar function.
chapter 10 library functions user ? s manual u15556ej1v0um 191 4-8 gets i/o functions function gets reads a character string. header stdio.h function prototype char *gets (char *s); function arguments return value gets s ... pointer to input character string normal ... s if the end of the file is detected without reading a character ... null pointer explanation  reads a character string using the getchar function and stores in the array that s indicates.  when the end of the file is detected ( getchar function returns ? 1) or when a line feed character is read, the reading of a character string ends. the line feed character read is abandoned, and a null character is written at the end of the character stored in the array in the end.  when the return value is normal, it returns s .  when the end of the file is detected and no character is read in the array, the contents of the array remain unchanged, and a null pointer is returned.
chapter 10 library functions user ? s manual u15556ej1v0um 192 4-9 putchar i/o functions function putchar outputs a character to sfr . header stdio.h function prototype int putchar (int c); function arguments return value putchar c ... character to be output character to have been output explanation  writes the character specified by c to the sfr symbol p0 (port 0) (converted to unsigned char type).  an error check related to writing is not performed.  to change the sfr to be written, it is necessary to either change the source and re-register to the library or user create a new putchar function.
chapter 10 library functions user ? s manual u15556ej1v0um 193 4-10 puts i/o functions function puts outputs a character string. header stdio.h function prototype int puts (const char *s); function arguments return value puts s ...pointer to an output character string normal ... 0 when putchar function returns ? 1 ... ? 1 explanation  writes the character string indicated by s using the putchar function and adds a line feed character at the end of the output.  writing of the null character at the end of the character string is not performed.  when the return value is normal, 0 is returned, and when the putchar function returns ? 1, ? 1 is returned.
chapter 10 library functions user ? s manual u15556ej1v0um 194 5-1 atoi, utility functions atol function the string function atoi converts the contents of a decimal integer string to an int value. the string function atol converts the contents of a decimal integer string to a long value. header stdlib. h function prototype int atoi (const char *nptr); long int atol (const char *nptr); function arguments return value atoi nptr ... string to be converted  int value if converted properly  int_max (32767) if positive overflow occurs  int_min ( ? 32768) if negative overflow occurs  0 if the string is invalid atol  long int value if converted properly  long_max (2147483647) for positive overflow  long_min ( ? 2147483648) for negative overflow  0 if the string is invalid
chapter 10 library functions user ? s manual u15556ej1v0um 195 atoi, utility functions atol explanation atoi  the atoi function converts the first part of the string pointed to by pointer ? nptr ? to an int value. the string may consist of zero or more white-space characters possibly followed by a minus or plus sign, followed by a string of digits.  the atoi function skips over zero or more white-space characters (for which isspace becomes true) from the beginning of the string and converts the string from the character next to the skipped white-spaces to an int value (until other than digits or a null character appears in the string).  if no digits to convert are found in the string, the function returns 0. if an overflow occurs, the function returns int_max (32767) for a positive overflow and int_min ( ? 32768) for a negative overflow. atol  the atol function converts the first part of the string pointed to by pointer ? nptr ? to a long value. the string may consist of zero or more white-space characters, possibly followed by a minus or plus sign, followed by a string of digits.  the atol function skips over zero or more white-space characters (for which isspace becomes true) from the beginning of the string and converts the string from the character next to the skipped white-spaces to a long value (until other than digits or null character appears in the string).  if no digits to convert are found in the string, the function returns 0. if an overflow occurs, the function returns long_max (2147483647) for a positive overflow and long_min ( ? 2147483648) for a negative overflow.
chapter 10 library functions user ? s manual u15556ej1v0um 196 5-2 strtol, utility functions strtoul function the string function strtol converts a string to a long integer. the string function strtoul converts a string to an unsigned long integer. header stdlib. h function prototype long int strtol (const char *nptr, char **endptr, int base); unsigned long int strtoul (const char *nptr, char **endptr, int base); function arguments return value strtol nptr ... string to be converted endptr ... address of char pointer base ... base for number represented in the string  long int value if converted properly  long_max (2147483647) for positive overflow  long_min ( ? 2147483648) for negative overflow  0 if not converted strtoul  unsigned long if converted properly  ulong_max (4294967295u) if overflow occurs  0 if not converted
chapter 10 library functions user ? s manual u15556ej1v0um 197 strtol, utility functions strtoul explanation strtol  the strtol function decomposes the string pointed by pointer nptr into the following three parts. (1) string of white-space characters that may be empty (to be specified by isspace ) (2) integer representation by the base determined by the value of ? base ? (3) string of one or more characters that cannot be recognized (including null terminators) the strtol function converts part (2) of the string into a long integer and returns this integer value.  a base of 0 indicates that the base should be determined from the leading digits of the string. a leading 0x or 0x indicates a hexadecimal number; a leading 0 indicates an octal number; otherwise, the number is interpreted as decimal. (in this case, the number may be signed.)  if the base is 2 to 36, the set of letters from a to z or a to z which can be part of a number (and which may be signed) with any of these bases are taken to represent 10 to 35. a leading 0x or 0x is ignored if the base is 16.  if endptr is not a null pointer, a pointer to part (3) of the string is stored in the object pointed to by endptr .  if the correct value causes an overflow, the function returns long_max (2147483647) for the positive overflow or long_min ( ? 2147483648) for the negative overflow depending on the sign and sets errno to erange (2).  if the string in (2) is empty or the first non-white-space character of the string (2) is not appropriate for an integer with the given base, the function performs no conversion and returns 0. in this case, the value of the string nptr is stored in the object pointed to by endptr (if it is not a null string). this holds true with the bases 0 and 2 to 36. strtoul  the strtoul function decomposes the string pointed by pointer nptr into the following three parts. (1) string of white-space characters that may be empty (to be specified by isspace ) (2) integer representation by the base determined by the value of base (3) string of one or more characters that cannot be recognized (including null terminators) the strtoul function converts part (2) of the string into a unsigned long integer and returns this unsigned long integer value.  a base of 0 indicates that the base should be determined from the leading digits of the string. a leading 0x or 0x indicates a hexadecimal number; a leading 0 indicates an octal number; otherwise, the number is interpreted as decimal.  if the base is 2 to 36, the set of letters from a to z or a to z which can be part of a number (and which may be signed) with any of these bases are taken to represent 10 to 35. a leading 0x or 0x is ignored if the base is 16.  if endptr is not a null pointer, a pointer to part (3) of the string is stored in the object pointed to by endptr .
chapter 10 library functions user ? s manual u15556ej1v0um 198 strtol, utility functions strtoul  if the correct value causes an overflow, the function returns ulong_max (4294967295u) and sets errno to erange (2).  if the string in (2) is empty or the first non-white-space character of the string in (2) is not appropriate for an integer with the given base, the function performs no conversion and returns 0. in this case, the value of the string nptr is stored in the object pointed to by endptr (if it is not a null string). this holds true with the bases 0 and 2 to 36.
chapter 10 library functions user ? s manual u15556ej1v0um 199 5-3 calloc utility functions function the memory function calloc allocates an array area and then initializes the area to 0. header stdlib. h function prototype void *calloc (size_t nmemb, size_t size); function arguments return value calloc nmemb ... number of members in the array size ... size of each member  pointer to the beginning of the allocated area if the requested size is allocated  null pointer if the requested size is not allocated explanation  the calloc function allocates an area for an array consisting of n number of members (specified by nmemb ), each of which has the number of bytes specified by size and initializes the area (array members) to zero.  if memory cannot be allocated, the function returns a null pointer. (this memory allocation will start from a break value and the address next to the allocated space will become a new break value. see 5-11 brk for break value setting with the memory function brk .)
chapter 10 library functions user ? s manual u15556ej1v0um 200 5-4 free utility functions function the memory function free releases the allocated block of memory. header stdlib. h function prototype void free (void *ptr); function arguments return value free ptr ... pointer to the beginning of block to be released none explanation  the free function releases the allocated space (before a break value) pointed to by ptr . ( malloc, calloc, or realloc called after free will allocate space that was freed earlier.)  if ptr does not point to the allocated space, free will take no action. (freeing the allocated space is performed by setting ptr as a new break value.)
chapter 10 library functions user ? s manual u15556ej1v0um 201 5-5 malloc utility functions function the memory function malloc allocates a block of memory. header stdlib. h function prototype void *malloc (size_t size); function arguments return value malloc size ... size of memory block to be allocated  pointer to the beginning of the allocated area if the requested size is allocated  null pointer if the requested size is not allocated explanation  the malloc function allocates a block of memory for the number of bytes specified by size and returns a pointer to the first byte of the allocated area.  if memory cannot be allocated, the function returns a null pointer. (this memory allocation will start from a break value and the address next to the allocated area will become a new break value. see 5-11 brk for break value setting with the memory function brk .)
chapter 10 library functions user ? s manual u15556ej1v0um 202 5-6 realloc utility functions function the memory function realloc reallocates a block of memory (namely, changes the size of the allocated memory). header stdlib. h function prototype void *realloc (void *ptr, size_t size); function arguments return value realloc ptr ... pointer to the beginning of block previously allocated size ... new size to be given to this block  pointer to the beginning of the reallocated space if the requested size is reallocated  pointer to the beginning of the allocated space if ptr is a null pointer  null pointer if the requested size is not reallocated or ? ptr ? is not a null pointer explanation  the realloc function changes the size of the allocated space (before a break value) pointed to by ptr to that specified by size .  if the value of size is greater than the size of the allocated space, the contents of the allocated space up to the original size will remain unchanged. the realloc function allocates only for the increased space. if the value of size is less than the size of the allocated space, the function will free the reduced space of the allocated space.  if ptr is a null pointer, the realloc function will newly allocate a block of memory of the specified size (same as malloc ).  if ptr does not point to the block of memory previously allocated or if no memory can be allocated, the function executes nothing and returns a null pointer. (reallocation will be performed by setting the address of ptr plus the number of bytes specified by size as a new break value.)
chapter 10 library functions user ? s manual u15556ej1v0um 203 5-7 abort utility functions function the program control function abort causes immediate, abnormal termination of a program. header stdlib. h function prototype void abort (void); function arguments return value abort none no return to its caller. explanation  the abort function loops and can never return to its caller.  the user must create the abort processing routine.
chapter 10 library functions user ? s manual u15556ej1v0um 204 5-8 atexit, utility functions exit function atexit registers the function called at the normal termination. exit terminates a program. header stdlib. h function prototype int atexit (void(*func) (void)); void exit (int status); function arguments return value atexit func ... pointer to function to be registered  0 if function is registered as wrap-up function  1 if function cannot be registered exit status ... status value indicating termination exit can never return. explanation atexit  the atexit function registers the wrap-up function pointed to by func so that it is called without argument upon normal program termination by calling exit or returning from main .  up to 32 wrap-up functions may be established. if the wrap-up function can be registered, atexit returns 0. if no more wrap-up functions can be registered because 32 wrap-up functions have already been registered, the function returns 1. exit  the exit function causes immediate, normal termination of a program.  this function calls the wrap-up functions in the reverse of the order in which they were registered with atexit .  the exit function loops and can never return to its caller.  the user must create the exit processing routine.
chapter 10 library functions user ? s manual u15556ej1v0um 205 5-9 abs, utility functions labs function the mathematical function abs returns the absolute value of its int type argument. the mathematical function labs returns the absolute value of its long type argument. header stdlib. h function prototype int abs (int j); long int labs (long int j); function arguments return value abs j ... any signed integer for which absolute value is to be obtained  absolute value of j if j falls within ? 32767 j 32767 ? 32768 (0x8000) if j is ? 32768 labs j ... any long integer for which absolute value is to be obtained  absolute value of j if j falls within ? 2147483647 j 2147483647 ? 2147483648 (0x80000000) if the value of j is ? 2147483648 explanation abs  the abs returns the absolute value of its int type argument. if j is ? 32768, the function returns ? 32768. labs  the labs returns the absolute value of its long type argument. if the value of j is ? 2147483648, the function returns ? 2147483648.
chapter 10 library functions user ? s manual u15556ej1v0um 206 5-10 div, utility functions ldiv function the mathematical function div performs the integer division of numerator divided by denominator. the mathematical function ldiv performs the long integer division of numerator divided by denominator. header stdlib.h function prototype div_t div (int numer, int denom); ldiv_t ldiv (long int numer, long int denom); function arguments return value div numer ... numerator of the division denom ... denominator of the division quotient to the quot element of structure type div_t and the remainder to the rem element ldiv quotient to the quot element of structure type ldiv_t and the remainder to the rem element explanation div  the div function performs the integer division of numerator divided by denominator. the result of div has a structure type named div_t with the elements quo (quotient) and rem (remainder).  the absolute value of the quotient is defined as the largest integer not greater than the absolute value of numer divided by the absolute value of denom . the remainder always has the same sign as the result of the division (plus if numer and denom have the same sign; otherwise minus).  the remainder is the value of numer - denom *quotient. if denom is 0, the quotient becomes 0 and the remainder becomes numer . if numer is ? 32768 and denom is ? 1, the quotient becomes ? 32768 and the remainder becomes 0. ldiv  the ldiv function performs the long integer division of numerator divided by denominator. the result of ldiv has a structure type named ? ldiv_t ? with the elements quo (quotient) and rem (remainder).  the absolute value of the quotient is defined as the largest long int type integer not greater than the absolute value of numer divided by the absolute value of denom . the remainder always has the same sign as the result of the division (plus if numer and denom have the same sign; otherwise minus).  the remainder is the value of numer - denom* quotient. if denom is 0, the quotient becomes 0 and the remainder becomes numer . if numer is ? 2147483648 and denom is ? 1, the quotient becomes ? 2147483648 and the remainder becomes 0.
chapter 10 library functions user ? s manual u15556ej1v0um 207 5-11 brk, utility functions sbrk function the memory function brk sets a break value. the memory function sbrk increments or decrements the set break value. header stdlib. h function prototype int brk (char *endds); char *sbrk (int incr); function arguments return value brk endds ... break value to be set  0 if break value is set properly ? 1 if break value cannot be changed sbrk incr ... value (bytes) by which set break value is to be incremented/decremented.  old break value if incremented or decremented properly ? 1 if old break value cannot be incremented or decremented explanation brk  the brk function sets the value given by endds as a break value (the address next to the end address of an allocated block of memory).  if endds is outside the permissible address range, the function sets no break value and sets errno to enomem (3). sbrk  the sbrk function increments or decrements the set break value by the number of bytes specified by incr . (increment or decrement is determined by the plus or minus sign of incr .)  if the incremented or decremented break value is outside the permissible address range, the function does not change the original break value and sets errno to enomem (3).
chapter 10 library functions user ? s manual u15556ej1v0um 208 5-12 atof utility functions strtod function atof converts a decimal integer character string to double . strtod converts a character string to double . header stdlib.h function prototype double atof const char *nptr) ; double strtod (const char *nptr, char **endptr) ; function arguments return value atof nptr ... character string to be converted endptr ... pointer to store a pointer to an unidentifiable area ( strtod only) ? normal ... converted value ? when positive overflow occurs ... huge_val (with the sign of the overflowed value) when negative overflow occurs ... 0 illegal character string ... 0 strtod nptr ... character string to be converted endptr ... pointer to store a pointer to an unidentifiable area ? normal ... converted value ? when positive overflow occurs ... huge_val (with the sign of the overflowed value) when negative overflow occurs ... 0 illegal character string ... 0
chapter 10 library functions user ? s manual u15556ej1v0um 209 5-12 atof utility functions strtod explanation atof  atof converts the character string that is pointed by the pointer nptr to double .  skips 0 or more strings of null characters (a character which makes isspace true) from the start and converts the character string (other than decimal characters or until the last null character appears) from the character next to the floating-point number.  if the conversion is performed correctly, a floating point number is returned.  if an overflow occurs in the conversion, huge_val , which has the sign of the overflowed value, is returned, and erange is set to errno .  if annihilation of valid digits occurs due to underflow or overflow, a non-normalized number and 0 are returned, respectively, and erange is set to errno .  if a conversion cannot be performed, 0 is returned. strtod  strtod converts the character string that is pointed by the pointer nptr to double .  skips 0 or more strings of null characters (a character which makes isspace true) from the start and converts the character string (other than decimal characters or until the last null character appears) from the character next to the floating-point number.  if the conversion is performed correctly, a floating-point number is returned.  if an overflow occurs in the conversion, huge_val , which has the sign of the overflowed value, is returned, and erange is set to errno .  if annihilation of valid digits occurs due to underflow or overflow, a non-normalized number and 0 are returned, respectively, and erange is set to errno . at the same time, endptr stores the pointer in the next character string.  if conversion cannot be performed, 0 is returned.
chapter 10 library functions user ? s manual u15556ej1v0um 210 5-13 itoa, utility functions ltoa, ultoa function the string function itoa converts an int integer to its string equivalent. the string function ltoa converts a long integer to its string equivalent. the string function ultoa converts an unsigned long integer to its string equivalent. header stdlib. h function prototype char *itoa (int value, char *string, int radix); char *ltoa (long value, char *string, int radix); char *ultoa (unsigned long value, char *string, int radix); function arguments return value itoa, ltoa, ultoa value ... string to which integer is to be converted string ... pointer to the conversion result radix ... base of output string  pointer to the converted string if converted properly  null pointer if not converted properly explanation itoa, ltoa, ultoa  the itoa, ltoa, and ultoa functions all convert the integer value specified by value to its string equivalent, which is terminated with a null character, and store the result in the area pointed to by ? string ? .  the base of the output string is determined by radix , which must be in the range 2 through 36. each function performs conversion based on the specified radix and returns a pointer to the converted string. if the specified radix is outside the range 2 through 36, the function performs no conversion and returns a null pointer.
chapter 10 library functions user ? s manual u15556ej1v0um 211 5-14 rand, utility functions srand function the mathematical function rand generates a sequence of psuedorandom numbers. the mathematical function srand sets a starting value (seed) for the sequence generated by rand . header stdlib. h function prototype int rand (void); void srand (unsigned int seed); function arguments return value rand none psuedorandom integer in the range of 0 to rand_max srand seed ... starting value for psuedorandom number generator none explanation rand  each time the rand function is called, it returns a psuedorandom integer in the range of 0 to rand_max . srand  the srand function sets a starting value for a sequence of random numbers. seed is used to set a starting point for a progression of random numbers that is a return value when rand is called. if the same seed value is used, the sequence of psuedorandom numbers is the same when srand is called again. calling rand before srand is used to set a seed is the same as calling rand after srand has been called with seed = 1. (the default seed is 1.)
chapter 10 library functions user ? s manual u15556ej1v0um 212 5-15 bsearch utility functions function the bsearch function performs a binary search. header stdlib. h function prototype void *bsearch (const void *key, const void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *)); function arguments return value bsearch key ... pointer to key for which search is made base ... pointer to sorted array that contains information to search nmemb ... number of array elements size ... size of an array compare ... pointer to function used to compare two keys  pointer to the first member that matches ? key ? if the array contains the key  null pointer if the key is not contained in the array explanation  the bsearch function performs a binary search on the sorted array pointed to by base and returns a pointer to the first member that matches the key pointed to by key . the array pointed to by base must be an array that consists of nmemb number of members each of which has the size specified by size and must have been sorted in ascending order.  the function pointed to by compare takes two arguments ( key as the 1st argument and array element as the 2nd argument), compares the two arguments, and returns: - negative value if the 1st argument is less than the 2nd argument - 0 if both arguments are equal - positive integer if the 1st argument is greater than the 2nd argument  when the -zr option is specified, the function passed to the argument of the bsearch function must be a pascal function.
chapter 10 library functions user ? s manual u15556ej1v0um 213 5-16 qsort utility functions function the qsort function sorts the members of a specified array using a quicksort algorithm. header stdlib. h function prototype void qsort (void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *)); function arguments return value qsort base ... pointer to array to be sorted nmemb ... number of members in the array size ... size of an array member compare ... pointer to function used to compare two keys none explanation  the qsort function sorts the members of the array pointed to by base in ascending order. the array pointed to by base consists of nmemb number of members each of that has the size specified by size .  the function pointed to by compare takes two arguments (array element 1 as the 1st argument and array element 2 as the 2nd argument), compares the two arguments, and returns: - negative value if the 1st argument is less than the 2nd argument - 0 if both arguments are equal - positive integer if the 1st argument is greater than the 2nd argument  if the two array elements are equal, the element nearest to the top of the array will be sorted first.  when the -zr option is specified, the function passed to the argument of the qsort function must be a pascal function.
chapter 10 library functions user ? s manual u15556ej1v0um 214 5-17 strbrk utility functions function strbrk sets a break value. header stdlib.h function prototype int strbrk (char *endds); function arguments return value strbrk endds ... break value to be set normal ... 0 when a break value cannot be changed ... ? 1 explanation  sets the value given by endds to the break value (the address following the address at the end of the area to be allocated).  when endds is out of the permissible range, the break value is not changed. enomem (3) is set to errno and ? 1 is returned.
chapter 10 library functions user ? s manual u15556ej1v0um 215 5-18 strsbrk utility functions function strsbrk increments/decrements a break value. header stdlib.h function prototype char *strsbrk (int incr); function arguments return value strsbrk incr ... amount by which a break value is to be incremented/decremented normal ... old break value when a break value cannot be incremented/decremened ... ? 1 explanation  incr byte increments/decrements a break value (depending on the sign of incr).  when the break value is out of the permissible range after incrementing/decrementing, the break value is not changed. enomem (3) is set to errno, and ? 1 is returned.
chapter 10 library functions user ? s manual u15556ej1v0um 216 &*   ,-      function stritoa converts int to a character string. strltoa converts long to a character string. strultoa converts unsigned long to a character string. header stdllib.h function prototype char *stritoa (int value, char *string, int radix); char *strltoa (long value, char *string, int radix); char *strultoa (unsigned long value, char *string, int radix); function arguments return value stritoa strltoa strultoa value ... character string to convert string ... pointer to conversion result radix ... radix to specify normal ... pointer to the converted character string other ... null pointer explanation stritoa, strltoa, strultoa  converts the specified numeric value value to the character string that ends with a null character, and stores the result in the area specified with string . the conversion is performed by the specified radix , and the pointer to the converted character string will be returned.  radix must be a value in the range of 2 to 36. in other cases, the conversion is not performed and a null pointer is returned.
chapter 10 library functions user?s manual u15556ej1v0um 217 6-1 memcpy, character string/memory functions memmove function the memory function memcpy copies a specified number of characters from a source area of memory to a destination area of memory. the memory function memmove is identical to memcpy , except that it allows overlap between the source and destination areas. header string. h function prototype void *memcpy (void *s1, const void *s2, size_t n); void *memmove (void *s1, const void *s2, size_t n); function arguments return value memcpy, memmove s1 ... pointer to object into which data is to be copied s2 ... pointer to object containing data to be copied n ... number of characters to be copied value of s1 explanation memcpy  the memcpy function copies n number of consecutive bytes from the object pointed to by s2 to the object pointed to by s1 .  if s2 < s1 < s2+n ( s1 and s2 overlap), the memory copy operation by memcpy is not guaranteed (because copying starts in sequence from the beginning of the area). memmove  the memmove function also copies n number of consecutive bytes from the object pointed to by s2 to the object pointed to by s1 .  even if s1 and s2 overlap, the function performs memory copying properly.
chapter 10 library functions user?s manual u15556ej1v0um 218 6-2 strcpy, character string/memory functions strncpy function the string function strcpy is used to copy the contents of one character string to another. the string function strncpy is used to copy up to a specified number of characters from one character string to another. header string. h function prototype char *strcpy (char *s1, const char *s2); char *strncpy (char *s1, const char *s2, size_t n); function arguments return value strcpy, strncpy s1 ... pointer to copy destination array s2 ... pointer to copy source array n ... number of characters to be copied value of s1 explanation strcpy  the strcpy function copies the contents of the character string pointed to by s2 to the array pointed to by s1 (including the terminating character).  if s2 < s1 (s2 + character length to be copied), the behavior of strcpy is not guaranteed (as copying starts in sequence from the beginning, not from the specified string). strncpy  the strncpy function copies up to the characters specified by n from the string pointed to by s2 to the array pointed to by s1 .  if s2 < s1 (s2 + character length to be copied or minimum value of s2 + n ? 1 ), the behavior of strncpy is not guaranteed (as copying starts in sequence from the beginning, not from the specified string).  if the string pointed by s2 is less than the characters specified by n , nulls will be appended to the end of s1 until n characters have been copied. if the string pointed to by s2 is longer than n characters, the resultant string that is pointed to by s1 will not be null terminated.
chapter 10 library functions user?s manual u15556ej1v0um 219 6-3 strcat, character string/memory functions strncat function the string function strcat concatenates one character string to another. the string function strncat concatenates up to a specified number of characters from one character string to another. header string. h function prototype char *strcat (char *s1, const char *s2); char *strncat (char *s1, const char *s2, size_t n); function arguments return value strcat, strncat s1 ... pointer to a string to which a copy of another string (s2) is to be concatenated s2 ... pointer to a string, copy of which is to be concatenated to another string (s1). n ... number of characters to be concatenated value of s1 explanation strcat  the strcat function concatenates a copy of the string pointed to by s2 (including the null terminator) to the string pointed to by s1 . the null terminator originally ending s1 is overwritten by the first character of s2 .  when copying is performed between objects overlapping each other, the operation is not guaranteed. strncat  the strncat function concatenates not more than the characters specified by n of the string pointed to by s2 (excluding the null terminator) to the string pointed to by s1 . the null terminator originally ending s1 is overwritten by the first character of s2 .  s1 must always be terminated with a null.  when copying is performed between objects overlapping each other, the operation is not guaranteed.
chapter 10 library functions user?s manual u15556ej1v0um 220 6-4 memcmp character string/memory functions function the memory function memcmp compares two data objects, with respect to a given number of characters. header string. h function prototype int memcmp (const void *s1, const void *s2, size_t n); function arguments return value memcmp s1, s2 ... pointers to two data objects to be compared n ... number of characters to compare 0 if s1 and s2 are equal  positive value if s1 is greater than s2 ; negative value if s1 is less than s2 ( s1 ? s2 ) explanation  the memcmp function compares the data object pointed to by s1 with the data object pointed to by s2 with respect to the number of bytes specified by n .  if the two objects are equal, the function returns 0.  the function returns a positive value if the object s1 is greater than the object s2 and a negative value if s1 is less than s2 .
chapter 10 library functions user?s manual u15556ej1v0um 221 6-5 strcmp, character string/memory functions strncmp function the string function strcmp compares two character strings. the string function strncmp compares not more than a specified number of characters from two character strings. header string. h function prototype char *strcmp (char *s1, const char *s2); char *strncmp (char *s1, const char *s2, size_t n); function arguments return value strcmp s1 ... pointer to one string to be compared s2 ... pointer to the other string to be compared 0 if s1 is equal to s2  integer less than 0 or greater than 0 if s1 is less than or greater than s2 ( s1 ? s2 ) strncmp s1 ... pointer to one string to be compared s2 ... pointer to the other string to be compared n ... number of characters to be compared 0 if s1 is equal to s2 within characters specified by n  integer less than 0 or greater than 0 if s1 is less than or greater than s2 ( s1 ? s2 ) within characters specified by n explanation strcmp  the strcmp function compares the two null terminated strings pointed to by s1 and s2 , respectively.  if s1 is equal to s2 , the function returns 0. if s1 is less than or grater than s2 , the function returns an integer less than 0 (a negative number) or greater than 0 (a positive number) ( s1 ? s2 ). strncmp  the strncmp function compares not more than the characters specified by n from the two null terminated strings pointed to by s1 and s2 , respectively.  if s1 is equal to s2 within the specified characters, the function returns 0. if s1 is less than or greater than s2 within the specified characters, the function returns an integer less than 0 (a negative number) or greater than 0 (a positive number) ( s1 ? s2 ).
chapter 10 library functions user?s manual u15556ej1v0um 222 6-6 memchr character string/memory functions function the memory function memchr converts a specified character to unsigned char , searches for it, and returns a pointer to the first occurrence of this character in an object of a given size. header string. h function prototype void *memchr (const void *s, int c, size_t n); function arguments return value memchr s ... pointer to objects in memory subject to search c ... character to be searched n ... number of bytes to be searched  pointer to the first occurrence of c if c is found  null pointer if c is not found explanation  the memchr function first converts the character specified by c to unsigned char and then returns a pointer to the first occurrence of this character within the n number of bytes from the beginning of the object pointed to by s .  if the character is not found, the function returns a null pointer.
chapter 10 library functions user?s manual u15556ej1v0um 223 6-7 strchr, character string/memory functions strrchr function the string function strchr returns a pointer to the first occurrence of a specified character in a string. the string function strrchr returns a pointer to the last occurrence of a specified character in a string. header string. h function prototype char *strchr (const char *s, int c); char *strrchr (const char *s, int c); function arguments return value strchr, strrchr s ... pointer to string to be searched c ... character specified for search  pointer indicating the first or last occurrence of c in string s if c is found in s  null pointer if c is not found in s explanation strchr  the strchr function searches the string pointed to by s for the character specified by c and returns a pointer to the first occurrence of c (converted to char type) in the string.  the null terminator is regarded as part of the string.  if the specified character is not found in the string, the function returns a null pointer. strrchr  the strrchr function searches the string pointed to by s for the character specified by c and returns a pointer to the last occurrence of c (converted to char type) in the string.  the null terminator is regarded as part of the string.  if no match is found, the function returns a null pointer.
chapter 10 library functions user?s manual u15556ej1v0um 224 6-8 strspn, character string/memory functions strcspn function the string function strspn returns the length of the initial substring of a string that is made up of only those characters contained in another string. the string function strcspn returns the length of the initial substring of a string that is made up of only those characters not contained in another string. header string. h function prototype size_t strspn (const char *s1, const char *s2); size_t strcspn (const char *s1, const char *2); function arguments return value strspn s1 ... pointer to string to be searched s2 ... pointer to string whose characters are specified for length of substring of the string s1 that is made up of only those characters contained in the string s2 strcspn match length of substring of the string s1 that is made up of only those characters not contained in the s2 explanation strspn  the strspn function returns the length of the substring of the string pointed to by s1 that is made up of only those characters contained in the string pointed to by s2 . in other words, this function returns the index of the first character in the string s1 that does not match any of the characters in the string s2 .  the null terminator of s2 is not regarded as part of s2 . strcspn  the strcspn function returns the length of the substring of the string pointed to by s1 that is made up of only those characters not contained in the string pointed to by s2 . in other words, this function returns the index of the first character in the string s1 that matches any of the characters in the string s2 .  the null terminator of s2 is not regarded as part of s2 .
chapter 10 library functions user?s manual u15556ej1v0um 225 6-9 strpbrk character string/memory functions function the string function strpbrk returns a pointer to the first character in a string to be searched that matches any character in a specified string. header string. h function prototype char *strpbrk (const char *s1, const char *s2); function arguments return value strpbrk s1 ... pointer to string to be searched s2 ... pointer to string whose characters are specified for match  pointer to the first character in the string s1 that matches any character in the string s2 if any match is found  null pointer if no match is found explanation  the strpbrk function returns a pointer to the first character in the string pointed to by s1 that matches any character in the string pointed to by s2 .  if none of the characters in the string s2 is found in the string s1 , the function returns a null pointer.
chapter 10 library functions user?s manual u15556ej1v0um 226 6-10 strstr character string/memory functions function the string function strstr returns a pointer to the first occurrence in the string to be searched of a specified string. header string. h function prototype char *strstr (const char *s1, const char *s2); function arguments return value strstr s1 ... pointer to string to be searched s2 ... pointer to specified string  pointer to the first appearance in the string s1 of the string s2 if s2 is found in s1  null pointer if s2 is not found in s1  value of s1 if s2 is a null string explanation  the strstr function returns a pointer to the first appearance in the string pointed to by s1 of the string pointed to by s2 (except the null terminator of s2 ).  if the string s2 is not found in the string s1, the function returns a null pointer.  if the string s2 is a null string, the function returns the value of s1 .
chapter 10 library functions user?s manual u15556ej1v0um 227 6-11 strtok character string/memory functions function the string function strtok returns a pointer to a token taken from a string (by decomposing it into a string consisting of characters other than delimiters). header string. h function prototype char *strtok (char *s1, const char *s2); function arguments return value strtok s1 ... pointer to string from which tokens are to be obtained or null pointer s2 ... pointer to string containing delimiters of token  pointer to the first character of a token if it is found  null pointer if there is no token to return explanation  a token is a string consisting of characters other than delimiters in the string to be specified.  if s1 is a null pointer, the string pointed to by the saved pointer in the previous strtok call will be decomposed. however, if the saved pointer is a null pointer, the function returns a null pointer without doing anything.  if s1 is not a null pointer, the string pointed to by s1 will be decomposed.  the strtok function searches the string pointed to by s1 for any character not contained in the string pointed to by s2 . if no character is found, the function changes the saved pointer to a null pointer and returns it. if any character is found, the character becomes the first character of a token.  if the first character of a token is found, the function searches for any characters contained in the string s2 after the first character of the token. if none of the characters is found, the function changes the saved pointer to a null pointer. if any of the characters is found, the character is overwritten by a null character and a pointer to the next character becomes a pointer to be saved.  the function returns a pointer to the first character of the token.
chapter 10 library functions user?s manual u15556ej1v0um 228 6-12 memset character string/memory functions function the memory function memset initializes a specified number of bytes in an object in memory with a specified character. header string. h function prototype void *memset (void *s, int c, size_t n); function arguments return value memset s ... pointer to object in memory to be initialized c ... character whose value is to be assigned to each byte n ... number of bytes to be initialized value of s explanation the memset function first converts the character specified by c to unsigned char and then assigns the value of this character to the n number of bytes from the beginning of the object pointed to by s .
chapter 10 library functions user?s manual u15556ej1v0um 229 6-13 strerror character string/memory functions function the strerror function returns a pointer to the location which stores a string describing the error message associated with a given error number. header string. h function prototype char *strerror (int errnum); function arguments return value strerror errnum ... error number  pointer to string describing error message if message associated with error number exists  null pointer if no message associated with error number exists explanation  the strerror function returns a pointer to one of the following strings associated with the value of errnum (error number): 0 ......................... ?error 0? 1 (edom) ........... ?argument too large? 2 (erange)....... ?result too large? 3 (enomem) ...... ?not enough memory? otherwise, the function returns a null pointer.
chapter 10 library functions user?s manual u15556ej1v0um 230 6-14 strlen character string/memory functions function the string function strlen returns the length of a character string. header string. h function prototype size_t strlen (const char *s); function arguments return value strlen s ... pointer to character string length of string s explanation the strlen function returns the length of the null terminated string pointed to by s .
chapter 10 library functions user?s manual u15556ej1v0um 231 6-15 strcoll character string/memory functions function strcoll compares two character strings based on the information specific to the locale. header string.h function prototype int strcoll (const char *s1, const char *s2) ; function arguments return value strcoll s1 ... pointer to comparison character string s2 ... pointer to comparison character string when character strings s1 and s2 are equal ... 0 when character strings s1 and s2 are different ... the difference between the values whose first different characters are converted to int (character of s1 ? character of s2 ) explanation  this compiler does not support operations specific to a cultural sphere. the operations are the same as that of strcmp .
chapter 10 library functions user?s manual u15556ej1v0um 232 6-16 strxfrm character string/memory functions function strxfrm converts a character string based on the information specific to the locale. header string.h function size_t strxfrm (char *s1, const char *s2, size_t n); function arguments return value strxfrm s1 ... pointer to a compared character string s2 ... pointer to a compared character string n ... maximum number of characters to s1 returns the length of the character string of the result of the conversion (does not include a character string to indicate the end) if the returned value is n or more, the contents of the array indicated by s1 is undefined. explanation ? this compiler does not support operations specific to a cultural sphere. the operations are the same as those of the following functions. strncpy ( s1 , s2 , c ) ; return ( strlen ( s2 )) ;
chapter 10 library functions user?s manual u15556ej1v0um 233 7-1 acos mathematical functions function acos finds acos. header math.h function prototype double acos (double x); function arguments return value acos x ... numeric value to perform operation when ?1 x 1 ... acos of x when x < ?1, 1 < x, x = nan ... nan explanation  calculates acos of x (range between 0 and p).  when x is non-numeric, nan is returned.  in the case of the definition area error of x < ?1, 1 < x, nan is returned and edom is set.
chapter 10 library functions user?s manual u15556ej1v0um 234 7-2 asin mathematical functions function asin finds asin. header math.h function prototype double asin (double x); function arguments return value asin x ... numeric value to perform operation when ?1 x 1 ... asin of x when x ?1, 1 x, x = nan ... nan when x = ?0 ... ?0 when underflow occurs ... non-normalized number explanation  calculates asin (range between ? /2 and + /2) of x.  in the case of area error of x < ?1, 1 < x, nan is returned and edom is set to errno .  when x is non-numeric, nan is returned.  when x is ?0, ?0 is returned.  if an underflow occurs as a result of conversion, a non-normalized number is returned.
chapter 10 library functions user?s manual u15556ej1v0um 235 7-3 atan mathematical functions function atan finds atan. header math.h function prototype double atan (double x); function arguments return value atan x ... numeric value to perform operation normal ... atan of x when x = nan ... nan when x = ?0 ... ?0 explanation  calculates atan (range between ? /2 and + /2) of x .  when x is non-numeric, nan is returned.  when x is ?0, ?0 is returned.  if an underflow occurs as a result of conversion, a non-normalized number is returned.
chapter 10 library functions user?s manual u15556ej1v0um 236 7-4 atan2 mathematical functions function atan2 finds atan of y/x. header math.h function prototype double atan2 (double y, double x); function arguments return value atan2 x ... numeric value to perform operation y ... numeric value to perform operation normal ... atan of y/x when both x and y are 0 or y/x is the value that cannot be expressed, or either x or y is nan and both x and y are ... nan non-normalized number ... when underflow occurs explanation ? atan (range between ? and + ) of y/x is calculated. when both x and y are 0 or y/x is the value that cannot be expressed, or when both x and y are infinite, nan is returned and edom is set to errno . ? if either x or y is non-numeric, nan is returned. ? if an underflow occurs as a result of the operation, a non-normalized number is returned.
chapter 10 library functions user?s manual u15556ej1v0um 237 7-5 cos mathematical functions function cos finds cos. header math.h function prototype double cos (double x); function arguments return value cos x ... numeric value to perform operation normal ... cos of x when x = nan, x = ... nan explanation  calculates cos of x .  if x is non-numeric, nan is returned.  if x is infinite, nan is returned and edom is set to errno .  if the absolute value of x is extremely large, the result of an operation becomes an almost meaningless value.
chapter 10 library functions user?s manual u15556ej1v0um 238 7-6 sin mathematical functions function sin finds sin header math.h function prototype double sin (double x); function arguments return value sin x ... numeric value to perform operation normal ... sin of x when x = nan, x = ... nan when underflow occurs ... non-normalized number explanation  calculates sin of x .  if x is non-numeric, nan is returned.  if x is infinite, nan is returned and edom is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if the absolute value of x is extremely large, the result of an operation becomes an almost meaningless value.
chapter 10 library functions user?s manual u15556ej1v0um 239 7-7 tan mathematical functions function tan finds tan. header math.h function prototype double tan (double x); function arguments return value tan x ... numeric value to perform operation normal ... tan of x when x = nan, x = ... nan when underflow occurs ... non-normalized number explanation  calculates tan of x .  if x is non-numeric, nan is returned.  if x is infinite, nan is returned and edom is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if the absolute value of x is extremely large, the result of an operation becomes an almost meaningless value.
chapter 10 library functions user?s manual u15556ej1v0um 240 7-8 cosh mathematical functions function cosh finds cosh. header math.h function prototype double cosh (double x) ; function arguments return value cosh x ... numeric value to perform operation normal ... cosh of x when overflow occurs, x = nan, x = ... huge_val (with positive sign) x = nan ... nan explanation  calculates cosh of x .  if x is non-numeric, nan is returned.  if x is infinite, a positive infinite value is returned.  if an overflow occurs as a result of the operation, huge_val with a positive sign is returned, and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 241 7-9 sinh mathematical functions function sinh finds sinh. header math.h function prototype double sinh (double x); function arguments return value sinh x ... numeric value to perform operation normal ... sinh of x when x = nan ... nan when x = ... when overflow occurs ... huge_val (with the sign of the overflowed value) when underflow occurs ... 0 explanation  calculates sinh of x .  if x is non-numeric, nan is returned.  if x is , is returned.  if an overflow occurs as a result of the operation, huge_val with the sign of the overflowed value is returned, and erange is set to errno .  if an underflow occurs as a result of the operation, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 242 7-10 tanh mathematical functions function tanh finds tanh. header math.h function prototype double tanh (double x); function arguments return value tanh x ... numeric value to perform operation normal ... tanh of x when x = nan ... nan when x = ... 1 when underflow occurs ... 0 explanation  calculates tanh of x .  if x is non-numeric, nan is returned.  if x is , 1 is returned.  if an underflow occurs as a result of the operation, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 243 7-11 exp mathematical function exp finds exponent function. header math.h function prototype double exp (double x); function arguments return value exp x ... numeric value to perform operation normal ... exponent function of x when x = nan ... nan when x = ... when overflow occurs ... huge_vqal (with positive sign) when underflow occurs ... non-normalized number when annihilation of valid digits occurs due to underflow ... +0 explanation  calculates the exponent function of x .  if x is non-numeric, nan is returned.  if x is , is returned.  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if annihilation of valid digits due to underflow occurs as a result of the operation, +0 is returned.  if an overflow occurs as a result of the operation, huge_val with a positive sign is returned and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 244 7-12 frexp mathematical functions function frexp finds the mantissa and exponent part. header math.h function prototype double frexp (double x, int *exp) ; function arguments return value frexp x ... numeric value to perform operation exp ... pointer to store exponent part normal ... mantissa of x when x = nan, x = ... nan when x = 0 ... 0 explanation  divides a floating-point number x into mantissa m and exponent n such as x = m*2^n and returns mantissa m .  exponent n is stored where the pointer exp indicates. the absolute value of m , however, is 0.5 or more and less than 1.0.  if x is non-numeric, nan is returned and the value of *exp is 0.  if x is infinite, nan is returned, and edom is set to errno with the value of *exp as 0.  if x is 0, 0 is returned and the value of *exp is 0.
chapter 10 library functions user?s manual u15556ej1v0um 245 7-13 ldexp mathematical functions function ldexp finds x*2^exp. header math.h function prototype double ldexp (double x, int exp); function arguments return value exp x ... numeric value to perform operation exp ... exponentiation normal ... x*2 ^ exp when x = nan ... nan when x = ... when x = 0 ... 0 when overflow occurs ... huge_val (with the sign of the overflowed value) when underflow occurs ... non-normalized number when annihilation of valid digits occurs due to underflow ... 0 explanation  calculates x*2^exp  if x is non-numeric, nan is returned  if x is , is returned.  if x is 0, 0 is returned.  if an overflow occurs as a result of the operation, huge_val with the overflowed value is returned and erange is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if annihilation of valid digits due to underflow occurs as a result of the operation, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 246 7-14 log mathematical functions function log finds the natural logarithm. header math.h function prototype double log (double x); function arguments return value log x ... numeric value to perform operation normal ... natural logarithm of x when x 0 ... huge_val (with negative sign) when x is non-numeric ... nan when x is infinite ... + explanation  finds the natural logarithm of x .  if x is non-numeric, nan is returned.  if x is + , + is returned.  in the case of an area error of x < 0, huge_val with a negative sign is returned, edom is set to errno .  if x = 0, huge_val with a negative sign is returned, and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 247 7-15 log10 mathematical functions function log10 finds the logarithm with 10 as the base. header math.h function prototype double log10 (double x) ; function arguments return value log10 x ... numeric value to perform operation normal ... logarithm with 10 of x as the base when x 0 ... huge_val (with negative sign) when x is non-numeric ... nan when x is infinite ... + explanation  finds the logarithm with 10 of x as the base.  if x is non-numeric, nan is returned.  if x is + , + is returned.  in the case of an area error of x < 0, huge_val with a negative sign is returned, edom is set to errno .  if x = 0, huge_val with a negative sign is returned, and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 248 7-16 modf mathematical functions function modf finds the fraction part and integer part. header math.h function prototype double modif (double x, double *iptr); function arguments return value modif x ... numeric value to perform operation iptr ... pointer to integer part normal ... fraction part of x when x is non-numeric or infinite ... nan when x is 0 ... 0 explanation  divides a floating-point number x into a fraction part and integer part  returns the fraction part with the same sign as that of x , and stores the integer part in the location indicated by the pointer iptr .  if x is non-numeric, nan is returned and stored in the location indicated by the pointer iptr .  if x is infinite, nan is returned and stored in the location indicated by the pointer iptr , and edom is set to errno .  if x = 0, 0 is stored in the location indicated by the pointer iptr .
chapter 10 library functions user?s manual u15556ej1v0um 249 7-17 pow mathematical functions function pow finds the yth power of x . header math.h function prototype double pow (double x, double y); function arguments return value pow x ... numeric value to perform operation y ... multiplier normal ... x^y either when x = nan or y = nan, x = + and y = 0 x < 0 and y integer, x < 0 and y = , x = 0 and y < 0 ... nan when underflow occurs ... non-normalized number when overflow occurs ... huge_val (with the sign of overflowed value) when annihilation of valid digits occurs due to underflow ... 0 explanation  calculates x^y.  if an overflow occurs as a result of the operation, huge_val with the sign of overflown value is returned, and erange is set to errno .  when x = nan or y = nan, nan is returned.  either when x = + and y = 0, x < 0 and y integer, x < 0 and y = or x = 0 and y 0, nan is returned and edom is set to errno .  if an underflow occurs, a non-normalized number is returned.  if annihilation of valid digits occurs due to underflow, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 250 7-18 sqrt mathematical functions function sqrt finds the square root. header math.h function prototype double sqrt (double x); function arguments return value sqrt x ... numeric value to perform operation when x 0 ... square root of x when x = 0 ... 0 when x < 0 ... nan explanation  calculates the square root of x .  in the case of an area error of x < 0, 0 is returned and edom is set to errno .  if x is non-numeric, nan is returned.  if x is 0, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 251 7-19 ceil mathematical function function ceil finds the minimum integer no less than x . header math.h function prototype double ceil (double x); function arguments return value ceil x ... numeric value to perform operation normal ... the minimum integer no less than x when x is non-numeric or x = ... nan when x = ?0 ... +0 when the minimum integer no less than x cannot be expressed ... x explanation  finds the minimum integer no less than x .  if x is non-numeric, nan is returned.  if x is ?0, +0 is returned.  if x is infinite, nan is returned and edom is set to errno .  if the minimum integer no less than x cannot be expressed, x is returned.
chapter 10 library functions user?s manual u15556ej1v0um 252 7-20 fabs mathematical functions function fabs returns the absolute value of the floating-point number x . header math.h function prototype double fabs (double x) ; function arguments return value fabs x ... numeric value to find the absolute value normal ... absolute value of x when x is non-numeric ... nan when x = ?0 ... +0 explanation  finds the absolute value of x .  if x is non-numeric, nan is returned.  if x is ?0, +0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 253 7-21 floor mathematical functions function floor finds the maximum integer no more than x . header math.h function prototype double floor (double x); function arguments return value floor x ... numeric value to perform operation normal ... the maximum integer no more than x when x is non-numeric or x = ... nan when x = ?0 ... +0 when the maximum integer no more than x cannot be expressed explanation  finds the maximum integer no more than x .  if x is non-numeric, nan is returned.  if x is ?0, +0 is returned.  if x is infinite, nan is returned and edom is set to errno .  if the maximum integer no more than x cannot be expressed, x is returned.
chapter 10 library functions user?s manual u15556ej1v0um 254 7-22 fmod mathematical functions function fmod finds the remainder of x/y . header math.h function prototype double fmod (double x, double y); function arguments return value fmod x ... numeric value to perform operation y ... numeric value to perform operation normal ... remainder of x/y when x is non-numeric or y is non-numeric, when y is 0, when x is ... nan when x and y = ... x explanation  calculates the remainder of x/y expressed with x ? i* y . i is an integer.  if y 0, the return value has the same sign as that of x and the absolute value is less than that of y .  if y is 0 or x = , nan is returned and edom is set to errno .  if x is non-numeric or y is non-numeric, nan is returned.  if y is infinite, x is returned unless x is infinite.
chapter 10 library functions user?s manual u15556ej1v0um 255 7-23 matherr mathematical functions function matherr performs exception processing of the library that deals with floating-point numbers. header math.h function prototype void matherr (struct exception *x) ; function arguments return value matherr struct exception { int type; char *name; } type ..... numeric value to indicate arithmetic exception name ... function name none explanation  when an exception is generated, matherr is automatically called in the standard and runtime libraries that deal with floating-point numbers.  when called from the standard library, edom and erange are set to errno . the following shows the relationship between the arithmetic exception type and errno. type arithmetic exception value set to errno 1 2 3 4 5 underflow annihilation overflow zero division inoperable erange erange erange edom edom original error processing can be performed by changing or creating matherr .
chapter 10 library functions user?s manual u15556ej1v0um 256 7-24 acosf mathematical functions function acosf finds acos . header math.h function prototype float acosf (float x); function arguments return value acosf x ... numeric value to perform operation when ?1 x 1 ... acos of x when x ?1, 1 < x, x = ... nan explanation  calculates acos (range between 0 and ) of x  if x is non-numeric, nan is returned.  in the case of a definition area error of x ?1, 1 x, nan is returned and edom is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 257 7-25 asinf mathematical functions function asinf finds asin . header math.h function prototype float asinf (float x); function arguments return value asinf x ... numeric value to perform operation when ?1 x 1 ... asin of x when x ?1, 1 < x, x = nan ... nan x = ?0 ... ?0 when underflow occurs ... non-normalized number explanation  calculates asin (range between ? /2 and + /2) of x  if x is non-numeric, nan is returned.  in the case of definition area error of x ?1, 1 x, nan is returned and edom is set to errno .  if x = ?0, ?0 is returned.  if an underflow occurs as a result of the operation, a non-normalized number is returned.
chapter 10 library functions user?s manual u15556ej1v0um 258 7-26 atanf mathematical functions function atanf finds atan . header math.h function prototype float atanf (float x); function arguments return value atanf x ... numeric value to perform operation normal ... atan of x when x = nan ... nan when x = ?0 ... ?0 explanation  calculates atan (range between ? /2 and + /2) of x  if x is non-numeric, nan is returned.  if x = ?0, ?0 is returned.  if an underflow occurs as a result of the operation, a non-normalized number is returned.
chapter 10 library functions user?s manual u15556ej1v0um 259 7-27 atan2f mathematical functions function atan2f finds atan of y/x . header math.h function prototype float atan2f (float y, float x); function arguments return value atan2f x ... numeric value to perform operation y ... numeric value to perform operation normal ... atan of y/x when both x and y are 0 or a value whose y/x cannot be expressed, or either x or y is nan, both x and y are ... nan when underflow occurs ... non-normalized number explanation  calculates atan (range between ? and + ) of y/x. when both x and y are 0 or the value whose y/x cannot be expressed, or when both x and y are infinite, nan is returned and edom is set to errno .  when either x or y is non-numeric, nan is returned.  if an underflow occurs as a result of the operation, a non-normalized number is returned.
chapter 10 library functions user?s manual u15556ej1v0um 260 7-28 cosf mathematical functions function cosf finds cos . header math.h function prototype float cost (float x); function arguments return value cosf x ... numeric value to perform operation normal ... cos of x when x = nan, x = ... nan explanation  calculates cos of x .  if x is non-numeric, nan is returned.  if x is infinite, nan is returned and edom is set to errno .  if the absolute value of x is extremely large, the result of an operation becomes an almost meaningless value.
chapter 10 library functions user?s manual u15556ej1v0um 261 7-29 sinf mathematical functions function sinf finds sin . header math.h function prototype float sinf (float x); function arguments return value sinf x ... numeric value to perform operation normal ... sin of x when x = nan , x = ... nan when underflow occurs ... non-normalized number explanation  calculates sin of x .  if x is non-numeric, nan is returned.  if x is infinite, nan is returned and edom is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if the absolute value of x is extremely large, the result of an operation becomes an almost meaningless value.
chapter 10 library functions user?s manual u15556ej1v0um 262 7-30 tanf mathematical functions function tanf finds tan . header math.h function prototype float tanf (float x); function arguments return value tanf x ... numeric value to perform operation normal ... tan of x when x = nan , x = ... nan when underflow occurs ... non-normalized number explanation  calculates tan of x .  if x is non-numeric, nan is returned.  if x is infinite, nan is returned and edom is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if the absolute value of x is extremely large, the result of an operation becomes an almost meaningless value.
chapter 10 library functions user?s manual u15556ej1v0um 263 7-31 coshf mathematical functions function coshf finds cosh . header math.h function prototype float coshf (float x) ; function arguments return value coshf x ... numeric value to perform operation normal ... cosh of x when overflow occurs, x = ... huge_val (with a positive sign) x = nan ... nan explanation  calculates cosh of x .  if x is non-numeric, nan is returned.  if x is infinite, positive infinite value is returned.  if an overflow occurs as a result of the operation, huge_val with a positive sign is returned and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 264 7-32 sinhf mathematical functions function sinhf finds sinh . header math.h function prototype float sinhf (float x); function arguments return value sinhf x ... numeric value to perform operation normal ... sinh of x when overflow occurs ... huge_val (with a sign of the overflowed value) x = nan ... nan when x = ... when underflow occurs ... 0 explanation  calculates sinh of x .  if x is non-numeric, nan is returned.  if x is , is returned.  if an overflow occurs as a result of the operation, huge_val with the sign of overflowed value is returned and erange is set to errno.  if an underflow occurs as a result of the operation, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 265 7-33 tanhf mathematical functions function tanhf finds tanh . header math.h function prototype float tanhf (float x); function arguments return value tanhf x ... numeric value to perform operation normal ... tanh of x x = nan ... nan when x = ... 1 when underflow occurs ... 0 explanation  calculates tanh of x .  if x is non-numeric, nan is returned.  if x is , 1 is returned.  if an underflow occurs as a result of the operation, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 266 7-34 expf mathematical functions function expf finds the exponent function. header math.h function prototype float expf (float x); function arguments return value expf x ... numeric value to perform operation normal ... exponent function of x when overflow occurs ... huge_val (with positive sign) x = nan ... nan when x = ... when underflow occurs ... non-normalized number when annihilation of valid digits occurs due to underflow ... +0 explanation  calculates exponent function of x .  if x is non-numeric, nan is returned.  if x is , is returned.  if an overflow occurs as a result of the operation, huge_val with a positive sign is returned and erange is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if annihilation of effective digits occurs due to underflow as a result of the operation, +0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 267 7-35 frexpf mathematical functions function frexpf finds the mantissa and exponent part. header math.h function prototype float frexpf (float x, int *exp) ; function arguments return value frexpf x ... numeric value to perform operation exp ... pointer to store exponent part normal ... mantissa of x when x = nan , x = ... nan when x = 0 ... 0 explanation  divides a floating-point number x into mantissa m and exponent n such as x = m *2^ n and returns mantissa m .  exponent n is stored in where the pointer exp indicates. the absolute value of m , however, is 0.5 or more and less than 1.0.  if x is non-numeric, nan is returned and the value of *exp is 0.  if x is , nan is returned, and edom is set to errno with the value of *exp as 0.  if x is 0, 0 is returned and the value of *exp is 0.
chapter 10 library functions user?s manual u15556ej1v0um 268 7-36 ldexpf mathematical functions function ldexpf finds x*2^exp. header math.h function prototype float ldexpf (float x, int exp); function arguments return value ldexpf x ... numeric value to perform operation exp ... exponentiation normal ... x*2^exp when x = nan ... nan when x = ... when x = 0 ... 0 when overflow occurs ... huge=val (with the sign of overflowed value) when underflow occurs ... non-normalized numberv when annihilation of valid digits occurs due to underflow ... 0 explanation  calculates x*2^exp.  if x is non-numeric, nan is returned. if x is , is returned. if x is 0, 0 is returned.  if overflow occurs as a result of operation, huge_val with the sign of overflowed value is returned and erange is set to errno .  if an underflow occurs as a result of the operation, a non-normalized number is returned.  if annihilation of valid digits due to underflow occurs as a result of the operation, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 269 7-37 logf mathematical functions function logf finds the natural logarithm. header math.h function prototype float logf (float x); function arguments return value logf x ... numeric value to perform operation normal ... natural logarithm of x when x is non-numeric ... nan when x is infinite ... + when x 0 ... huge_val (with negative sign) explanation  finds natural logarithm of x .  if x is non-numeric, nan is returned.  if x is + , + is returned.  in the case of an area error of x < 0, huge_val with a negative sign is returned, and edom is set to errno .  if x = 0, huge_val with a negative sign is returned, and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 270 7-38 log10f mathematical functions function log10f finds the logarithm with 10 as the base. header math.h function prototype float log10f (float x); function arguments return value log10f x ... numeric value to perform operation normal ... logarithm with 10 of x as the base when x is non-numeric ... nan when x = + ... + when x 0 ... huge_val (with negative sign) explanation  finds the logarithm with 10 of x as the base.  if x is non-numeric, nan is returned.  if x is + , + is returned.  in the case of an area error of x < 0, huge_val with a negative sign is returned, and edom is set to errno.  if x = 0, huge_val with a negative sign is returned, and erange is set to errno .
chapter 10 library functions user?s manual u15556ej1v0um 271 7-39 modff mathematical functions function modff finds the fraction part and integer part. header math.h function prototype float modff (float x, float *iptr); function arguments return value modff x ... numeric value to perform operation iptr ... pointer for integer part normal ... fraction part of x when x is non-numeric or infinite ... nan when x = 0 ... 0 explanation  divides a floating-point number x into a fraction part and integer part.  returns the fraction part with the same sign as that of x , and stores the integer part in the location indicated by the pointer iptr .  if x is non-numeric, nan is returned and stored in the location indicated by the pointer iptr . if x is infinite, nan is returned and stored in the location indicated by the pointer iptr, and edom is set to errno .  if x = 0, 0 is returned and stored in the location indicated by the pointer iptr .
chapter 10 library functions user?s manual u15556ej1v0um 272 7-40 powf mathematical functions function powf finds the yth power of x. header math.h function prototype float powf (float x, float y); function arguments return value powf x ... numeric value to perform operation y ... multiplier normal ... x^y either when = x = nan or y = nan x = + and y = 0 x < 0 and y integer, x < 0 and y = x = 0 and y 0 ... nan when underflow occurs ... non-normalized number when overflow occurs ... huge_val (with the sign of overflowed value) when annihilation of valid digits occurs due to underflow ... 0 explanation  calculates x^y .  if an overflow occurs as a result of the operation, huge_val with the sign of overflowed value is returned, and erange is set to errno .  when x = nan or y = nan , nan is returned.  either when x = + and y = 0, x < 0 and y integer, x < 0 and y = , or x = 0 and y 0, nan is returned and edom is set to errno .  if an underflow occurs, a non-normalized number is returned.  if annihilation of valid digits occurs due to underflow, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 273 7-41 sqrtf mathematical functions function sqrtf finds the square root. header math.h function prototype float sqrtf (float x); function arguments return value sqrtf x ... numeric value to perform operation when x 0 ... square root of x when x = 0 ... 0 when x < 0 ... nan explanation  calculates the square root of x .  in the case of area error of x < 0, 0 is returned and edom is set to errno .  if x is non-numeric, nan is returned.  if x is 0, 0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 274 7-42 ceilf mathematical functions function ceilf finds the minimum integer no less than x. header math.h function prototype float ceilf (float x); function arguments return value ceilf x ... numeric value to perform operation normal ... the minimum integer no less than x when x is non-numeric or x = ... nan when x = ?0 ... +0 when the minimum integer no less than x cannot be expressed ... x explanation  finds the minimum integer no less than x .  if x is non-numeric, nan is returned.  if x is ?0, +0 is returned. if x is infinite, nan is returned and edom is set to errno .  if the minimum integer no less than x cannot be expressed, x is returned.
chapter 10 library functions user?s manual u15556ej1v0um 275 7-43 fabsf mathematical functions function fabsf returns the absolute value of the floating-point number x . header math.h function prototype float fabsf (float x); function arguments return value fabsf x ... numeric value to find the absolute value normal ... absolute value of x when x is non-numeric ... nan when x = ?0 ... +0 explanation  finds the absolute value of x .  if x is non-numeric, nan is returned.  if x is ?0, +0 is returned.
chapter 10 library functions user?s manual u15556ej1v0um 276 7-44 floorf mathematical functions function floorf finds the maximum integer no more than x . header math.h function prototype float floorf (float x); function arguments return value floorf x ... numeric value to perform operation normal ... the maximum integer no more than x when x is non-numeric or infinite ... nan when x = ?0 ... +0 when the maximum integer no more than x cannot be expressed ... x explanation  finds the maximum integer no more than x .  if x is non-numeric, nan is returned.  if x is ?0, +0 is returned.  if x is infinite, nan is returned and edom is set to errno .  if the maximum integer no more than x cannot be expressed, x is returned.
chapter 10 library functions user?s manual u15556ej1v0um 277 7-45 fmodf mathematical functions function fmodf finds the remainder of x/y . header math.h function prototype float fmodf (float x, float y); function arguments return value fmodf x ... numeric value to perform operation y ... numeric value to perform operation normal ... remainder of x/y when x is non-numeric or y is non-numeric when y is 0, when x is ... nan when x and y = ... x explanation  calculates the remainder of x/y expressed with x ? i* y. i is an integer.  if y 0, the return value has the same sign as that of x and the absolute value is less than y .  if y is 0 or x = , nan is returned and edom is set to errno .  if x is non-numeric or y is non-numeric, nan is returned.  if y is infinite, x is returned unless x is infinite.
chapter 10 library functions user?s manual u15556ej1v0um 278 8-1 _ _ assertfail diagnostic functions function _ _ assertfail supports the assert macro. header math.h function prototype int _ _assertfail (char*_ _msg, char*_ _cond, char*_ _file, int_ _line); function arguments return value _ _assertfail _ _msg ... pointer to character string to indicate output conversion specification to be passed to printf function _ _cond ... actual argument of assert macro _ _file ... source file name _ _line ... source line number undefined explanation the _ _ assertfail function receives information from the assert macro (refer to 10.2 headers (13) assert.h ), calls the printf function, outputs information, and calls the abort function. the assert macro adds diagnostic functions to a program. when an assert macro is executed, if p is false (equal to 0), an assert macro passes information related to the specific call that has brought the false value (actual argument text, source file name, and source line number are included in the information. the other two are the values of macro _file_ _ and _ _line_ _ , respectively) to the _ _assertfail function.
chapter 10 library functions user?s manual u15556ej1v0um 279 10.5 batch files for update of startup routine and library functions this compiler is provided with batch files for updating a part of the standard library functions and the startup routine. the batch files in the bat directory are shown in table 10-3 below. caution the file d4025.78k in the bat directory is used during batch file activation for updating the library, not for development. when developing a system, it is necessary to have a device file (sold separately). table 10-3. batch files for updating library functions batch file application mkstup.bat updates the startup routine (cstart*.asm). when changing the startup routine, perform assembly using this batch file. reprom.bat updates the firmware rom termination routine (rom.asm). when changing rom.asm, update the library using this batch file. repgetc.bat updates the getchar function. the default assumption sets p0 of the sfr to input port. when it is necessary to change this setting, change the defined value of equ of port in getchar.asm and update the library using this batch file. repputc.bat updates the putchar function. the default assumption sets p0 of the sfr to output port. when it is necessary to change this setting, change the defined value of equ of port in putchar.asm and update the library using this batch file. repputcs.bat updates the putchar function to sm78k4-supporting. when it is necessary to check the output of the putchar function using the sm78k4, update the library using this batch file. repselo.bat saves/restores the reserved area of the compiler ( _@kregxx ) as part of the save/restore processing of the setjmp/longjmp functions (the default assumption is to not save/restore). update the library using this batch file when the -qr option is specified. repselon.bat does not save/restore the reserved area of the compiler ( _@kregxx ) as part of the save/restore processing of the setjmp/longjmp functions (the default assumption is to not save/restore). update the library using this batch file when the -qr option is not specified. repvect.bat updates the address value setting processing of the branch table of the interrupt vector table allocated in the flash area (vect*.asm). the default assumption sets the top address of the flash area branch table to 4000h. when it is necessary to change this setting, change the defined value of equ of itbltop in vect.inc and update the library using this batch file.
chapter 10 library functions user?s manual u15556ej1v0um 280 10.5.1 using batch files use the batch files in the subdirectory bat. because these files are the batch files used to activate the assembler and librarian, an environment in which the assembler package ra78k4 ver. 1.50 or later operates is necessary. before using the batch files, set the directory that contains the ra78k4 execution format file using the environment variable path. create a subdirectory (lib) of the same level as bat for the batch files and put the post-assembly files in this subdirectory. when a c startup routine or library is installed in a subdirectory lib that is the same level as bat, these files are overwritten. to use the batch files, move the current directory to the subdirectory bat and execute each batch file. at this time, the following parameters are necessary. product type = chiptype (classification of target chip) 4026 pd784026, etc. the following is an illustration of how to use each batch file. the batch file for: (1) startup routine ? for pc-9800 series, ibm pc/at tm and compatibles mkstup chiptype example mkstup 4026 ? for hp9000 series 700?, sparcstation? family /bin/sh mkstup.sh chiptype example /bin/sh mkstup.sh 4026 (2) firmware rom routine update ? for pc-9800 series, ibm pc/at and compatibles reprom chiptype example reprom 4026 ? for hp9000 series 700, sparcstation family /bin/sh reprom.sh chiptype example /bin/sh reprom.sh 4026
chapter 10 library functions user?s manual u15556ej1v0um 281 (3) getchar function update ? for pc-9800 series, ibm pc/at and compatibles repgetc chiptype example repgetc 4026 ? for hp9000 series 700, sparcstation family /bin/sh repgetc.sh chiptype example /bin/sh repgetc.sh 4026 (4) putchar function update ? for pc-9800 series, ibm pc/at and compatibles repputc chiptype example repputc 4026 ? for hp9000 series 700, sparcstation family /bin/sh repputc.sh chiptype example /bin/sh repputc.sh 4026 (5) putchar function (sm78k4-supporting) update ? for pc-9800 series, ibm pc/at and compatibles repputcs chiptype example repputcs 4026 ? for hp9000 series 700, sparcstation family /bin/sh repputcs.sh chiptype example /bin/sh repputcs.sh 4026
chapter 10 library functions user?s manual u15556ej1v0um 282 (6) setjmp/longjmp function update (with restore/save processing) ? for pc-9800 series, ibm pc/at and compatibles repselo chiptype example repselo 4026 ? for hp9000 series 700, sparcstation family /bin/sh repselo.sh chiptype example /bin/sh repselo.sh 4026 (7) setjmp/longjmp function update (without restore/save processing) ? for pc-9800 series, ibm pc/at and compatibles repselon chiptype example repselon 4026 ? for hp9000 series 700, sparcstation family /bin/sh repselon.sh chiptype example /bin/sh repselon.sh 4026 (8) interrupt vector table update ? for pc-9800 series, ibm pc/at and compatibles repvect chiptype example repvect 4026 ? for hp9000 series 700, sparcstation family /bin/sh repvect.sh chiptype example /bin/sh repvect.sh 4026
user?s manual u15556ej1v0um 283 chapter 11 extended functions this chapter describes the extended functions unique to this c compiler and not specified in the ansi (american national standards institute) standard for c. the extended functions of this c compiler are used to generate codes for effective utilization of the target devices in the 78k/iv series. not all of these extended functions are always effective. therefore, it is recommended to use only the effective ones according to the purpose of use. for the effective use of the extended functions, refer to chapter 13 effective utilization of compiler along with this chapter. c source programs created by using the extended functions of the c compiler utilize microcontroller-dependent functions. as regards portability to other microcontrollers, they are compatible at the c language level. for this reason, c source programs developed by using these extended functions are portable to other microcontrollers with easy-to-make modifications. remark in the explanation of this chapter, ?rtos? indicates the 78k/iv series real-time os.
chapter 11 extended functions user ? s manual u15556ej1v0um 284 11.1 macro names this c compiler has two types of macro names: those indicating the series name for target devices and those indicating device name (processor type). these macro names are specified according to the option for compilation to output object code for a specific target device or according to the processor type in the c source. in the example below, _ _k4_ _ and _ _4026_ are specified. for details of these macro names, see 9.8 compiler-defined macro names. [example] option for compilation >cc78k4 -c4026 prime.c ? specification of device type: #pragma pc (4026) 11.2 keywords the following tokens are added to this c compiler as keywords to realize the extended functions. similarly to ansi-c keywords, these tokens cannot be used as labels or as variable names. all the keywords must be described in lowercase letters. a keyword containing an uppercase letter is not interpreted as a keyword by the c compiler. this following shows the list of keywords added to this compiler. of these keywords, ones not starting with ? _ _ ? can be disabled by specifying the option ( -za ) that enables only ansi-c language specifications (for the ansi-c keywords, refer to 2.1 keywords ).
chapter 11 extended functions user?s manual u15556ej1v0um 285 table 11-1. list of added keywords keyword use _ _ _ _ _ _ _ _ callt callt callt/ _ _ _ _ _ _ _ _ callt functions _ _ _ _ _ _ _ _ callf callf callf/ _ _ _ _ _ _ _ _ callf functions _ _ _ _ _ _ _ _ sreg sreg sreg/ _ _ _ _ _ _ _ _ sreg variables _ _ _ _ _ _ _ _ sreg1 _ _ _ _ _ _ _ _ sreg1 variables noauto noauto functions _ _ _ _ _ _ _ _ leaf norec norec/ _ _ _ _ _ _ _ _ leaf functions _ _ _ _ _ _ _ _ boolean boolean boolean type/ _ _ _ _ _ boolean type bit bit type variables _ _ _ _ _ _ _ _ boolean1 _ _ _ _ _ _ _ _ boolean1 type variables _ _ _ _ _ _ _ _ interrupt hardware interrupt _ _ _ _ _ _ _ _ interrupt _ _ _ _ brk software interrupt _ _ _ _ _ _ _ _ asm asm statements _ _ _ _ _ _ _ _ rtos _ _ _ _ interrupt handler to allocate for rtos _ _ _ _ _ _ _ _ pascal pascal function _ _ _ _ _ _ _ _ flash firmware rom function _ _ _ _ _ _ _ _ directmap absolute address allocation specification (1) functions the keywords callt, _ _callt, callf _ _callf, noauto, norec, _ _leaf, _ _interrupt, _ _interrupt_brk, _ _rtos_interrupt , and _ _ flash are attribute qualifiers. these keywords must be described before any function declaration. the format of each attribute qualifier is shown below. attribute-qualifier ordinary-declarator function-name (parameter type list/identifier list) _ _callt int func (int);
chapter 11 extended functions user?s manual u15556ej1v0um 286 attribute qualifier specifications are limited to those listed below. (the noauto and norec/_ _leaf qualifiers cannot be specified at the same time.) callt and _ _callt , callf and _ _callf , norec and _ _leaf are regarded as the same specifications. however, qualifiers that include ?_ _? are enabled even when the -za option is specified. ? callt  callf  noauto  norec  callt noauto  callt norec  noauto callt  norec callt  callf noauto  callf norec  noauto callf  norec callf  _ _interrupt  _ _interrupt_brk  _ _rtos_interrupt  _ _pascal  _ _pascal noauto  _ _pascal callt  _ _pascal callf  noauto_ _pascal  callt_ _pascal  callf_ _pascal  callt noauto_ _pascal  callf noauto_ _pascal  _ _flash (2) variables  the keyword sreg, _ _sreg, or _ _sreg1 is specified in a similar manner to the register storage class specifier of c. (for details, see 11.5 (3) how to use the saddr area. )  the keyword bit , boolean, _ _boolean, or _ _boolean1 is specified in a similar manner to the char or int type specifier of c. however, these types can be specified only for the variables defined outside a function (external variables).  the same regulations apply to the _ _directmap specification as to the type qualifiers in c language (refer to 11.5 (42) absolute address allocation specification for details).
chapter 11 extended functions user?s manual u15556ej1v0um 287 11.3 memory the memory model is determined by the memory space of the target device. (1) memory model a maximum of 1 mb of program memory space and a maximum of 16 mb of data memory space are available (for the memory map, refer to the user?s manual of each target device). this compiler has the three types of memory models: small, medium, and large. objects are changed and output by specifying each memory model option. for details of each model, refer to table 11-2 . table 11-2. memory model memory model (option) explanation small model ( -ms ) a model with a combined code/data block capacity of 64 kb. medium model ( -mm ) a model with a capacity of up to 1 mb for the code block and 64 kb for the data block large model ( -ml ) a model with a combined code/data block capacity of 16 mb, including up to 1 mb for the code block and 16 mb for the data block. (2) register bank  the register bank is set to ?rb0? at startup (set in the startup routine of this compiler). register bank 0 is made always used (unless the register bank is changed) by this setting.  the specified register bank is set at the start of the interrupt function that has specified the change of the register bank. (3) location function  with the large model or medium model, the location function ( -cs option) allows changing the location of the internal ram (including saddr area and sfr area) between 64 kb (location 00h) and 1024 kb (location 0fh) (with the small model, the location of the internal ram is fixed to 64 kb). for the -cs option, refer to the cc78k4 c compiler operation user?s manual (u15557e) .
chapter 11 extended functions user?s manual u15556ej1v0um 288 (4) memory space this c compiler uses memory space as shown in table 11-3 below. table 11-3. utilization of memory space address use size (bytes) 00 40 to 7fh callt table 64 0800 to 0fffh callf entry 2048 (f)fd 20 to dfh sreg variables, boolean type variables 192 (f)fd 20 to ffh arguments of norec functions note 1 8 automatic variables of norec functions note 1 8 register variables note 1 16 (f)fe 00 to 7fh sreg1 variables, boolean1 type variables 128 (f)fe 80 to efh rb7 to rb1 note 2 (work registers) 112 f0 to ffh rb0 (work registers) 16 (f)ff 00 to ffh sfr variables 256 notes 1. the restore to this area is not processed within the interrupt function when the -qr option is not specified (default). this reduces the preprocessing/postprocessing of interrupt functions and allows users to use the areas of note 1 as sreg variable or boolean type variable areas when using a real- time os, etc. for the save/restore processing code output, refer to 11.5 (10) interrupt function . this area, as shown in appendix a list of labels for saddr area , defines labels and secures areas in a library. standard library functions setjmp , longjmp refer to a part of this area _@kreg00 . 2. used when a register bank is specified. consecutive 32-byte area in the interval above
chapter 11 extended functions user?s manual u15556ej1v0um 289 11.4 #pragma directives the #pragma directives are preprocessing directives supported by ansi. a #pragma directive, depending on the character string to follow #pragma , instructs the compiler to translate using the method determined by the compiler. if the compiler does not support #pragma directives, the #pragma directive is ignored and compilation is continued. if keywords are added by a directive, an error is output if the c source includes the keywords. in order to avoid this, the keywords in the c source should either be deleted or sorted by the #ifdef directive. this c compiler supports the following #pragma directives to realize the extended functions. the keywords specified after #pragma can be described either in uppercase or lowercase letters. for the extended functions using #pragma directives, refer to 11.5 how to use extended functions .
chapter 11 extended functions user?s manual u15556ej1v0um 290 table 11-4. list of #pragma directives #pragma directive applications #pragma sfr describes sfr name in c 11.5 (4) how to use the sfr area #pragma asm inserts asm statement in c source 11.5 (9) asm statements #pragma vect #pragma interrupt describes interrupt processing in c 11.5 (10) interrupt functions #pragma di #pragma ei describes di/ei instructions in c 11.5 (12) interrupt functions #pragma halt #pragma stop #pragma nop #pragma brk describes cpu control instructions in c 11.5 (13) cpu control instruction #pragma access uses absolute address access functions 11.5 (17) absolute address access function #pragma section changes compiler output section name and specifies section location 11.5 (19) changing compiler output section name #pragma name changes module name 11.5 (21) module name changing function #pragma rot uses rotate function 11.5 (22) rotate function #pragma mul uses multiplication function 11.5 (23) multiplication function #pragma div uses division function 11.5 (24) division function #pragma opc uses data insertion function 11.5 (25) data insertion function #pragma rtos_interrupt uses interrupt handler for real-time os (rx78k/iv) 11.5 (26) interrupt handler for real-time os (rtos) #pragma rtos_task uses task function for real-time os (rx78k/iv) 11.5 (28) task function for real-time os (rtos) #pragma ext_table specifies the first address of the flash area branch table 11.5 (34) flash area branch table #pragma ext_func calls a function to the flash area from the boot area 11.5 (35) function call function from the boot area to the flash area . #pragma inline expands the standard library functions memcpy and memset inline 11.5 (38) memory manipulation function #pragma addnaccess uses 3-byte address reference/generation function 11.5 (41) three-byte address reference/generation function
chapter 11 extended functions user?s manual u15556ej1v0um 291 11.5 how to use extended functions this section describes the extended functions in the following format. function : outlines the function that can be implemented with the extended function. effect : explains the effect brought about by the extended function. usage : explains how to use the extended function. example : gives an application example of the extended function. restrictions : explains restrictions if any on the use of the extended function. explanation : explains the above application example. compatibility : explains the compatibility of a c source program developed by another c compiler when it is to be compiled with this c compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 292 (1) callt functions callt functions callt/ _ _ _ _ _ _ _ _ callt function  the callt instruction stores the address of a function to be called in an area [40h to 7fh] called the callt table, so that the function can be called with a shorter code than the one used to call the function directly.  to call a function declared by the callt (or _ _ callt ) (called the callt function), a name with ? prefixed to the function name is used. to call the function, the callt instruction is used.  the function to be called is not different from the ordinary function. effect the object code can be shortened. usage add the callt/_ _ callt attribute to the function to be called as follows (described at the beginning). callt extern type-name function-name _ _callt extern type-name function-name example _ _callt void func1 (void) ; _ _callt void func1 (void) { : /* function body */ : }
chapter 11 extended functions user?s manual u15556ej1v0um 293 callt functions callt/ _ _ _ _ _ _ _ _ callt restrictions  the address of each function declared with callt/_ _ callt will be allocated to the callt table at the time of linking object modules. for this reason, when using the callt table in an assembler source module, the routine to be created must be made ?relocatable? using symbols.  a check on the number of callt functions is made at linking time.  when the -za option is specified, _ _callt is enabled and callt is disabled.  when the -zf option is specified, callt functions cannot be defined. if a callt function is defined, an error will occur.  the area of the callt table is 40f to 70f.  when the callt table is used exceeding the number of callt attribute functions permitted, a compilation error will occur.  the callt table is used by specifying the -ql option. for that reason, the number of callt attributes permitted per load module and the total in the linking modules is as shown in table 11-5. table 11-5. number of callt attribute functions that can be used when ? ? ? ? ql option is specified number of functions that can be used memory model -ql1 -ql2 -ql3 -ql4 small model 32 32 15 10 medium model 32 25 8 3 large model 32 23 6 1  cases in which the -ql option is not used and the defaults are as shown below. table 11-6. restriction on callt function usage callt function restriction value number per load module 32 max. total number in linked module 32 max.
chapter 11 extended functions user?s manual u15556ej1v0um 294 example (c source) ============ ca1.c ============ ============ ca2.c ============ _ _callt extern int tsub (); void main () _ _callt int tsub () { { int ret_val; int val; ret_val = tsub(); return val; } } (output object of assembler) ca1 module extrn ?tsub ; declaration callt [?tsub] ; call ca2 module public _tsub ; declaration public ?tsub ; @@calt cseg callt0 ; allocation to segment ?tsub: dw _tsub @@code cseg _tsub: ; function definition : function body : explanation the callt attribute is given to the function tsub() so that it can be stored in the callt table. compatibility from another c compiler to this c compiler  modification is not required if the keyword callt /_ _ callt is not used.  when changing functions to callt functions, use the method above. from this c compiler to another c compiler  #define must be used. for details, see 11.6 modifications of c source.
chapter 11 extended functions user?s manual u15556ej1v0um 295 (2) register variables register variables register function  allocates the declared variables (including arguments of function) to the register (rp3, vp) and saddr2 area (_@kreg00 to _@kreg15 ). saves and restores registers or saddr2 area during the preprocessing/ postprocessing of the module that declared a register.  when the -zo option is specified, register variables are allocated in the order of declaration. when the -zo option is not specified (default), on the other hand, the allocation is performed based on the number of references. therefore, it is undefined to which register or saddr2 area the register variable is allocated. for details of the allocation of register variables, refer to 11.7 function call interface .  register variables are allocated to different areas depending on the compilation condition as shown below (for each option, refer to the cc78k4 c compiler operation user?s manual (u15557e) ). 1. register variables are allocated to saddr2 area only when the -qr option is specified. 2 when the -qf option is specified and the -zo option is not specified, register variables are also allocated also to register up . 3. when neither the -zo option nor the -qf option is specified, all the register arguments and register variables are allocated to registers and saddr2 area. when there is no argument or automatic variable allocated to the stack area (that is, a stack frame is not generated), register variables are also allocated to register up (when the -ml option is specified and the -qr option is not specified, however, register variables are allocated only if the total size allocated to the register is 6 bytes or less assuming the pointer is 3 bytes).
chapter 11 extended functions user?s manual u15556ej1v0um 296 register variables register these are summarized in table 11-7. table 11-7. registers to allocate register variables without ?zo option specification registers to allocate without -qr rp3, vp with ?qr rp3, vp, saddr2 area (_@kreg00 to _@kreg15) with -qf *1 rp3, vp, up without -qf and a stack frame not generated *2 rp3, vp, up above *1 or *2 and with -qr rp3, vp, up, saddr2 area (_@kreg00 to _@kreg15) with ?zo option specification registers to allocate without -qr rp3, vp with -qr rp3, vp, saddr2 area (_@kreg00 to _@kreg15) with -qf *1 rp3, vp without -qf and a stack frame not generated *2 rp3, vp above *1 or *2 and with -qr rp3, vp, saddr2 area (_@kreg00 to _@kreg15) effect  instructions to the variables allocated to the register or saddr2 area are generally shorter in code length than those to memory. this helps shorten object code and also improves program execution speed.
chapter 11 extended functions user?s manual u15556ej1v0um 297 register variables register usage declare a variable with the register storage class specifier as follows. register type-name variable-name example void main (void) { register unsigned char c; : } restrictions  if register variables are not used so frequently, object code may increase (depending on the size and contents of the source).  register variable declarations may be used for char/int/short/long/float/double/long double and pointer data types.  with the medium model, function pointers are allocated to saddr2 area for register variables. function pointers cannot be allocated to registers. a char type register variable uses only half the space required for the register variable of any other type. a long/float/double/long double type variable uses twice the space.  the function pointer type of the medium model and the pointer of the large model use one and a half the amount of space.  all the types have byte boundaries.  if the register variables exceed the ?usable number? shown in table 11-8 , they are handled the same as automatic variables without a register storage class specifier and allocated to ordinary memory space.  up to 20 bytes or 22 bytes can be allocated as register variables (6 bytes when 16 bytes of saddr2 area and 4 bytes of registers or up are used). table 11-8. restrictions on register variables usage data type usable number (per function) int/short 10 variables max. function pointer of medium model 5 variables max. pointer of large model 6 variables max. long/float/double/long double 5 variables max.
chapter 11 extended functions user?s manual u15556ej1v0um 298 register variables register example 1 1. example of register variable allocation to register (with the large model, and when the optimization option is the default) (c source 1) void main () { register int i, j; j = 0; j = 1; i + = j; } (output object of compiler) @@code cseg _main: push uup ; saves register contents at the beginning of the function. push rp3 ; subw rp3, rp3 ; assigns 0 to i movw up, #01h ; assigns 1 to j addw rp3, up ; assigns i to the result of i + j pop rp3 ; restores register contents at the end of the function. pop uup ; ret
chapter 11 extended functions user?s manual u15556ej1v0um 299 register variables register example 2 2. example of register variable allocation to register and saddr2 area (with the large model, and when the optimization option -qr is specified) (c source 2) void main () { register unsigned int a, b, c, d; d = a - b; d = b - c; } (output object of compiler) extrn saddr2(_@kreg00) ; performs reference declaration of saddr2 area to be used. public _main @@code cseg _main; push uup ; saves register contents at the beginning of the function. push rp3 ; push vvp ; movw ax, _@kreg00 ; saves contents of saddr2 are at the beginning of the function. push ax movw ax, rp3 ; subw ax, up ; a ? b movw vp, ax ; assigns the result of a ? b to d movw ax, up subw ax, _@kreg00 ; b ? c movw vp, ax ; assigns the result of b ? c to d pop ax movw _@kreg00, ax ; restores contents of saddr2 area at the end of the function. pop vvp pop rp3 pop uup ; restores register contents at the end of the function. ret
chapter 11 extended functions user ? s manual u15556ej1v0um 300      explanation  to use register variables, you only need to declare them with the register storage class specifier.  label _@kreg00 , etc. includes the modules declared with public in the library attached to this c compiler. compatibility from another c compiler to this c compiler  modification is not required if the other c compiler supports register declarations.  when changing to register variables, add the register declarations for the variables to the program. from this c compiler to another c compiler  modification is not required if the other compiler supports register declarations.  how many variable registers can be used and to which area they will be allocated depends on the implementation of the other c compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 301 (3) how to use the saddr area usage of saddr area sreg/_ _sreg (1) usage with sreg declaration function  the external variables and in-function static variables ( sreg variables) declared with the keyword sreg or _ _sreg are automatically allocated to saddr2 [xfd20h to xfdffh] area with relocatability (x: 0 or f by specifying location). when those variables exceed the area shown above, a compilation error occurs.  the sreg variables are treated in the same manner as the ordinary variables in the c source.  each bit of sreg variables of char, short, int, and long type becomes a boolean type variable automatically.  sreg variables declared without an initial value take 0 as the initial value.  the area of sreg variables declared in the assembler source that can be referenced is the saddr2 area [xfd20h to xfdffh]. when the -qr option is specified, however, the compiler may use up to 32 mb of saddr2 area, so care must be taken (refer to table 11-3 utilization of memory space ). effect  instructions to the saddr2 area are generally shorter in code length than those to memory. this helps shorten object code and also improves program execution speed. usage  declare variables with the keywords sreg and _ _ _ _ _ _ _ _ sreg inside a module and a function that defines the variables. only a variable with a static storage class specifier can become a sreg variable inside a function. sreg type-name variable-name / sreg static type-name variable-name _ _sreg type-name variable-name / _ _sreg static type-name variable-name  declare the following variables inside a module that refers to sreg external variables. they can be described inside a function as well. extern sreg type-name variable-name / extern _ _sreg type-name variable-name
chapter 11 extended functions user?s manual u15556ej1v0um 302 usage of saddr area sreg/_ _sreg restrictions if const type is specified, or if sreg/_ _sreg is specified for a function, a warning message is output, and the sreg declaration is ignored. arguments of functions and automatic variables cannot be specified to this area. char type uses half the space of other types and long/float/double/long double types use twice the space. function pointers of the medium model and the large model use one and a half the amount of space as other types. all the types have byte boundaries. when -za is specified, only _ _sreg is enabled and sreg is disabled. the following shows the maximum number of sreg variables that can be used per load module. table 11-9. restrictions on sreg variable usage data type usable number of sreg variables (per load module) int/short max. 112 (96 when -qr is specified) note function pointer of medium model max. 74 (64 when -qr is specified) note pointer of large model max. 74 (64 when -qr is specified) note note when the -qr option is not specified, the reserved area for the argument of the norec function/automatic variables and register variables (32 bytes of saddr2 area) can be used as sreg variable area. when bit and boolean type variables are used, the usable number is decreased. example the following shows an example when the large model is used. (c source) extern sreg int hsmm0; extern sreg int hsmm1; extern sreg int *hsptr; void main ( ) { hsmm0 -= hsmm1; }
chapter 11 extended functions user?s manual u15556ej1v0um 303 usage of saddr area sreg/_ _sreg (assembler source) the following example shows a definition code for an sreg variable that the user creates. if an extern declaration is not made in the c source, the c compiler outputs the following codes. in this case, the org quasi directive will not be output. public _hsmm0 ; declaration public _hsmm1 ; public _hsptr ; @@dats dseg saddr2 ; allocation to segment org 0ffd20h ; _hsmm0: ds (2) ; _hsmm1: ds (2) ; _hsptr: ds (3) ; (output object of compiler) extrn saddr2(_hsmm1) extrn saddr2(_hsmm0) public _main @@code cseg _main: subw _hsmm0, _hsmm1 ret compatibility from another c compiler to this c compiler  modification is not required if the other compiler does not use the keyword sreg / _ _sreg . when changing to sreg variable, use the method above. from this c compiler to another c compiler  modifications are made by #define . for details, refer to 11.6 modifications of c source . by this modification, sreg variables are handled as ordinary variables.
chapter 11 extended functions user?s manual u15556ej1v0um 304 usage of saddr area -rd (2) usage with saddr automatic allocation option of external variables/external static variables function  external variables/external static variables (except const type) are automatically allocated to the saddr2 area regardless of whether an sreg declaration is made or not.  depending on the value of n, the external variables and external static variables to allocate can be specified as follows. table 11-10. variables allocated to saddr2 area by -rd option value of n variables allocated to saddr2 area if 1 variables of char and unsigned char types if 2 variables if n is 1 and variables of short, unsigned short, int, unsigned int, enum , small model pointer, and medium model data pointer type if 4 variables if n is 2 and variables of long, unsigned long, float, double, long double , medium model pointer, and large model pointer type if omitted all variables (including structures, unions, and arrays in this case only)  variables declared with the keyword sreg are allocated to the saddr2 area, regardless of the above specification.  the above rule also applies to variables referenced by an extern declaration, and processing is performed as if these variables were allocated to the saddr2 area.  the variables allocated to the saddr2 area by this option are treated in the same manner as the sreg variable. the functions and restrictions of these variables are as described in (1). method of specification specify the -rd [n] ( n : 1, 2, or 4) option. restrictions  with the -rd [n] option, modules specifying different n values cannot be linked to each other.
chapter 11 extended functions user?s manual u15556ej1v0um 305 usage of saddr area -rs (3) usage with saddr automatic allocation option of internal static variables function  automatically allocates internal static variables (except const type) to saddr2 area regardless of an sreg declaration.  depending on the value of n, the internal static variables to allocate can be specified as follows. table 11-11. variables allocated to saddr2 area by -rs option value of n variables allocated to saddr2 area if 1 variables of char and unsigned char types if 2 variables if n is 1 and variables of short , unsigned short , int , unsigned it , enum , small model pointer, and medium model data pointer type if 4 variables if n is 2 and variables of long , unsigned long , float , double , long double , medium model function pointer, and large model pointer type if omitted all variables (including structures, unions, and arrays in this case only)  variables declared with the keyword sreg are allocated to the saddr2 area regardless of the above specification.  the variables allocated to the saddr2 area by this option are handled in the same manner as the sreg variable. the functions and restrictions for these variables are as described in (1). method of specification specify the -rs [n] ( n : 1, 2, or 4) option. remark with the -rs [n] option, modules specifying different n values can also be linked to each other.
chapter 11 extended functions user?s manual u15556ej1v0um 306 usage of saddr area _ _sreg1 (4) usage with _ _sreg1 declaration function  variables declared with the keyword _ _sreg1 (called sreg1 variables) are automatically allocated to saddr1 [xfe00h to xfe7fh] area (x: 0 or f by specifying location) with relocatability. when the sreg1 variable exceeds the area shown above, a compilation error occurs.  saddr1 area [xfe00h to xfeffh] can be used as sreg1 variables by changing the location of segments in the assembler source or at the time of linking. however, care must be taken because the compiler uses the area [xfe80h to xfeffh] as a general-purpose register area.  the sreg1 variables are handled in the same manner as ordinary variables in the c source.  each bit of sreg1 variables of char/short/int/long type automatically becomes a _ _boolean1 type variable.  sreg1 variables declared without an initial value take 0 as the initial value. effect  instructions to the saddr1 area are generally shorter in code length than those to memory. this helps shorten object code and also improves program execution speed. usage  declare a variable with the keyword _ _sreg1 inside the module in which the variable is to be defined. _ _sreg1 type-name variable-name  declare the following variables inside the module in which the sreg1 variable is referenced. extern _ _sreg1 type-name variable-name
chapter 11 extended functions user?s manual u15556ej1v0um 307 usage of saddr area _ _sreg1 restrictions  when _ _sreg1 type is specified for a const type or function, a warning message is output and the _ _sreg1 declaration is ignored.  arguments of functions and automatic variables cannot be specified to this area.  char type uses half the space of other types, and long/float/double/long double types use twice the space.  all the types have byte boundaries. medium model function pointers and large model pointers use one and a half the space of other types. the following shows the maximum number of sreg variables that can be used per load module. table 11-12. restrictions on sreg1 variable usage data type usable number of sreg variables (per load module) int/short max. 64 note medium model function pointer max. 42 note large model pointer max. 42 note note saddr1 area [xfe00h to xfe7fh] is used. when _ _boolean1 type variables are used, the usable number is decreased. example the following shows an example when the large model is used. (c source) extern _ _sreg1 int s1; extern _ _sreg1 int s2; extern _ _sreg1 int *spr; void main( ) { s1 -= s2; }
chapter 11 extended functions user?s manual u15556ej1v0um 308 usage of saddr area _ _sreg1 (assembler source) the following example shows a definition code for a sreg1 variable that the user creates. if an extern declaration is not made in the c source, the c compiler outputs the codes in the same way as those of assembler source. in this case, the org quasi directive will not be output. public _s1 ; declaration public _s2 ; public _sptr ; @@dats1 dseg saddr ; allocation to segment org 0ffe00h ; _s1: ds (2) ; _s2: ds (2) ; _sptr: ds (3) ; (output object of compiler) extrn _s2 extrn _s1 public _main @@code cseg _main: subw _s1,_s2 ret compatibility from another c compiler to this c compiler  modification is not required if the keyword _ _sreg1 is not used in the program.  when changing to sreg1 variables, use the method above. from this c compiler to another c compiler  #define must be used. for details, see 11.6 modifications of c source . by this modification, sreg1 variables will be handled as ordinary variables.
chapter 11 extended functions user?s manual u15556ej1v0um 309 (4) how to use the sfr area usage of sfr area sfr function  the sfr area refers to a group of special function registers such as mode registers and control registers for the various peripherals of the 78k/iv series microcontrollers.  by declaring use of sfr names, manipulations on the sfr area can be described at the c source level.  sfr variables are external variables without initial values (undefined).  a write check will be performed on read-only sfr variables.  a read check will be performed on write-only sfr variables.  assignment of illegal data to an sfr variable will result in a compilation error.  the sfr names that can be used are those allocated to an area consisting of addresses ff00h to ffffh with the small model, or xff00h to xffffh with the medium large model. (x: 0 or f by specifying location) effect  manipulations on the sfr area can be described at the c source level.  instructions to the sfr area are shorter in code length than those to memory. this helps shorten object code and also improves program execution speed. usage  declare the use of an sfr name in the c source with the #pragma preprocessing directive, as follows. (the keyword sfr can be described in uppercase or lowercase letters.). #pragma sfr  the #pragma sfr directive must be described at the beginning of the c source line. if #pragma pc (processor type) is specified, however, describe #pragma sfr after that. the following statement and directives may precede the #pragma sfr directive. . comment statement . preprocessing directives that do not define or refer to a variable or function  in the c source program, describe an sfr name that the device has as is (without change). in this case, the sfr need not be declared.
chapter 11 extended functions user?s manual u15556ej1v0um 310 usage of sfr area sfr restrictions  all sfr names must be described in uppercase letters. lowercase letters are treated as ordinary variables. example (c source) #ifdef _ _k4_ _ #pragma sfr #endif void main () { cmk00 = 1; pm0 = 0x11; p0 = 10; : } (output object of compiler) the c compiler outputs no declaration-related code but outputs the following code inside the function. @@code cseg _main: set1 cic00.6 mov pm0, #011h ;17 sub p0, #0ah ;10 ret
chapter 11 extended functions user?s manual u15556ej1v0um 311 usage of sfr area sfr explanation  in the above example, use of sfr variables is declared with the #pragma sfr directive. by this declaration, special function registers such as p0 (port 0) and cic00 (one of the interrupt control registers  ) can be used. note bit 6 of the cic00 register has the sfr bit name cmk00. for sfr , refer to the user?s manual of the target device used. compatibility from another c compiler to this c compiler  modification is not required if those portions of the c source program do not depend on the device or compiler. from this c compiler to another c compiler  delete the #pragma sfr statement or sort by #ifdef and add the declaration of the variable that was formerly an sfr variable. the following shows an example. #ifdef _ _k4_ _ #pragma sfr #else /* declaration of variables */ unsigned char p0; #endif void main(void) { p0 = 0; }  in the case of a device that has the sfr or its alternative functions, a dedicated library must be created to access that area.
chapter 11 extended functions user?s manual u15556ej1v0um 312 (5) noauto function noauto function noauto function  the noauto function sets restrictions for automatic variables not to output the codes of preprocessing/ postprocessing (generation of stack frame).  all the arguments are allocated to registers. if there is an argument that cannot be allocated to registers, a compilation error occurs. (a) when -zo option is specified  arguments are passed via registers.  the locations where arguments are passed to the function call side and the function definition side become the locations where arguments are allocated.  the save and restore of the register to which arguments are allocated are performed before/after the function call.  automatic variables cannot be used.  arguments are allocated in the same order as ordinary functions.  table 11-13 shows the registers to which the arguments of the noauto function are passed/allocated. table 11-13. registers used for noauto function arguments (with -zo) data type first argument second argument third argument or later char r6 r7 r8, r9, r10, r11 int, short rp3 vp up (only when ? ? ? ? ms ? ? ? ? qf is specified) long/float/double/ long double vp (higher 16 bits) rp3 (lower 16 bits) small model pointer vp up (only when ? ? ? ? qf is specified) rp3 large model pointer vvp
chapter 11 extended functions user?s manual u15556ej1v0um 313 noauto function noauto (b) when -zo option is not specified  arguments are passed on the function call side in the same manner as ordinary functions (refer to 11.7.2 ordinary function call interface ).  the arguments passed via a register or stack are copied to the register shown in table 11-14 on the function definition side (copying register is necessary even when an argument is passed via a register because the registers of the function call side and the function definition side are different).  the save and restore of registers to which arguments are allocated are performed on the function definition side. table 11-14. registers used for noauto function arguments (without -zo) data type first argument second argument third argument or later char (with 4-byte argument) note char (without 4-byte argument) note r10 r6 r11 r7 r6, r7, r8, r9, r10, r11, r8, r9 int, short, enum (with 4-byte argument) note (without 4-byte argument) note up rp3 rp3 up vp vp long/float/double/long double vp (higher 16 bits) rp3 (lower 16 bits) small model pointer medium model data pointer up vp rp3 large model pointer uup vvp note 4-byte arguments are arguments of long, float, double, long double type remarks 1. the medium model function pointer cannot be used as an argument to be allocated to a register. 2 . the order of the register allocation in this function is the same as the order when the -qf option is specified in ordinary functions.
chapter 11 extended functions user?s manual u15556ej1v0um 314 noauto function noauto  automatic variables can be used only when all the automatic variables can be allocated to the registers remaining after the argument allocation and to the saddr2 area ( _@kregxx ) for register variables. however, automatic variables are allocated to the saddr2 area for register variables only when the -qr option is specified during compilation. if the -qro option is specified during compilation, a warning message is output and automatic variables are not allocated to saddr2 area.  automatic variables are allocated in the same order as arguments are allocated. the automatic variables allocated to saddr2 area ( _@kregxx ) are allocated in the order of declaration (if they are not allocated, a compilation error occurs).  the save and restore of _@kregxx , the register to which automatic variables are allocated, are performed on the function definition side. effect  the object code can be shortened and execution speed can be improved. usage declare a function with the noauto attribute in the function declaration, as follows. noauto type-name function-name restrictions  when the -zo option is specified, automatic variables cannot be used inside the noauto function, and neither can the register variables.  when the -za option is specified, the noauto function is disabled.  the arguments and automatic variables of the noauto function (only when the -zo option is specified) have restrictions on their types and numbers. the following shows the types of arguments that can be used inside a noauto function.  pointer  char / signed char/ unsigned char  int / signed int / unsigned int  short / signed short / unsigned short  enum  long / signed long / unsigned long  float / double / long double
chapter 11 extended functions user?s manual u15556ej1v0um 315 noauto function noauto table 11-15. restrictions on noauto function arguments (with -zo) data type restriction type other than pointer max. 4 bytes (max. 6 bytes) note small model pointer max. 4 bytes (max. 6 bytes) note medium model data pointer max. 4 bytes large model pointer max. 1 variable note up to 6 bytes can be used only when the -ms and -qf options are specified. table 11-16. restrictions on noauto function arguments and automatic variables (without -zo) data type restriction type other than pointer max. 6 bytes (max. 22 bytes) note 1 small model pointer medium model data pointer max. 6 bytes (max. 22 bytes) note 1 medium model function pointer (max. 5 variables) note 2 large model pointer max. 2 variables (max. 7 variables) note 3 notes 1. when the -qr option is specified, only automatic variables can be used up to 22 bytes. 2. when the -qr option is specified, only automatic variables can be used up to 5 variables. the medium model function pointer cannot be used as a register argument (not allocated to registers). 3. when the -qr option is specified, only automatic variables can be used up to 7 variables.  these restrictions are checked during compilation.  if arguments and automatic variables are declared with a register (only when the -zo is not specified), the register declaration is ignored.
chapter 11 extended functions user?s manual u15556ej1v0um 316 noauto function noauto example (c source) noauto short nfunc (short, short, short); short l, m; void main (void) { static short s1, s2, s3; l = nfunc (s1, s2, s3); } noauto short nfunc(short a, short b, short c) { m = a + b + c; rturn(m); } (output object of compiler) with small model, when -zo option is not specified @@data dseg _l : ds (2) _m : ds (2) ?l0003: ds (2) ?l0004: ds (2) ?l0005: ds (2) @@codes cseg base _main: s3 push ax movw ax,!?l0004 ; s2 push ax movw ax,!?l0003 ; s1 call !_nfunc ; calls nfunc (a, b, c) pop ax,de movw !_l,bc ; assigns return value to external variable l ret
chapter 11 extended functions user?s manual u15556ej1v0um 317 noauto function noauto (output object of compiler ? continued) _nfunc: push rp3,vp,up ; saves register for arguments movw rp3,ax ; assigns first argument a to rp3 movw ax,[sp+9] ; assigns second argument b to up movw up,ax ; movw ax,[sp+11] ; assigns third argument c to vp movw vp,ax ; movw ax,rp3 ; to a (rp3) addw ax,up ; adds b (up) addw ax,vp ; adds c (vp) movw !_m,ax ; assigns the result of operation to external variable m movw bc,ax ; returns external variable m pop rp3,vp,up ; restores register for arguments g explanation  in the above example, the noauto attribute is added at the header part of the c source. noauto is declared and stack frame formation is not performed. compatibility from another c compiler to this c compiler  modification is not required if the keyword noauto is not used.  when changing variables to noauto variables, modify the program according to the method above. from this c compiler to another c compiler  #define must be used. for details, see 11.6 modifications of c source.
chapter 11 extended functions user?s manual u15556ej1v0um 318 (6) norec function norec function norec function  a function that does not call another function by itself can be changed to a norec function.  with the norec function, code for preprocessing and postprocessing (stack frame formation) is not output.  all the arguments of norec function are allocated to registers and saddr2 area ( _@nrargx ) for arguments of the norec function. when the -qr option is not specified during compilation (default), however, saddr2 area is not used.  if arguments cannot be allocated to registers and saddr2 area, a compilation error occurs. (a) when -zo option is specified  arguments are passed via a register and saddr2 area ( _@nrargx ). when a register is used, arguments are stored in the same manner as the noauto function (refer to table 11-13 ).  if arguments cannot be passed via a register, a register is not used, but arguments are passed via saddr2 area ( _@nrargx ) (a register and saddr2 area are not used simultaneously). when saddr2 area is used, arguments are sequentially stored in ascending order from _@nrarg0 starting from the first argument.  the locations where arguments are passed on the function call side and the function definition side become the locations where arguments are allocated.  the save and restore of the register to which arguments are allocated are performed before/after the function call.  automatic variables are allocated to saddr2 area ( _@nratxx ), and so are the register variables. they are allocated in the sequence they have been declared in ascending order starting from _@nrat00 . if there are excess registers for arguments, automatic variables are allocated to registers first. however, automatic variables are allocated to saddr2 area only when the -qr option is specified. if automatic variables cannot be allocated to registers or saddr2 area, a compilation error occurs.  the save and restore of the register to which automatic variables are allocated are performed on the function definition side.
chapter 11 extended functions user?s manual u15556ej1v0um 319 norec function norec (b) when -zo option is not specified on the function call side, arguments are passed via a register and saddr2 area ( _@nrargx ) for the arguments of norec functions. on the function definition side, the arguments passed via a register are copied to a register (because the registers of the function call side and the function definition side are different). if arguments are passed via saddr2 area, the location where arguments are passed becomes the location where arguments are allocated. arguments are allocated to registers first, and then the arguments that cannot be allocated to registers are allocated to saddr2 area. the save and restore of registers to store arguments are performed on the function definition side. automatic variables are allocated to registers or to saddr2 area ( _@nrargx ) for the arguments of the norec function if registers can be used. if the areas above cannot be used, automatic variables are allocated to saddr2 area ( _@nratxx ) for the automatic variables of the norec function in the sequence they have been declared and in ascending order. the following shows the registers to be used for passing the arguments of norec functions. table 11-17. registers used for norec function arguments: passing side (without -zo) data type first argument second argument third argument or later char a c de, rp2, saddr2 note int, short, enum ax de rp2, saddr2 note long/float/double/ long double de (higher 16 bits) ax (lower 16 bits) saddr note saddr2 small model pointer medium model data pointer ax de rp2, saddr2 note large model pointer tde saddr2 note saddr2 note note when the -qr option is specified, there arguments can be passed via _@nrargx (saddr2) . medium model function pointers (3 bytes) cannot be used as the arguments to be allocated to registers.
chapter 11 extended functions user?s manual u15556ej1v0um 320 norec function norec table 11-18. registers used for norec function arguments: receiving side (without -zo) data type first argument second argument third argument or later char (with 4-byte arguments) note 1 char (without 4-byte arguments) note 1 r10 r6 r11 r7 r6, r7, r8, r9, saddr2 note 2 r10, r11, r8, r9, saddr2 note 2 int, short, enum (without 4-byte arguments) note 1 (with 4-byte arguments) note 1 up rp3 rp3 up vp, saddr2 note 2 vp, saddr2 note 2 long/float/double/long double vp (higher16 bits) rp3 (lower 16 bits) saddr2 note 2 saddr2 note 2 small model pointer medium model data pointer up vp rp3, saddr2 note 2 large model pointer vvp saddr2 note 2 saddr2 note 2 notes 1 4-byte arguments are arguments of long , float , double and long double type 2 when the -qr option is specified, these arguments can be passed via _ @nrargx (saddr2) . the medium model?s function pointer (3 bytes) cannot be used as an argument assigned to the register. cautions 1. the medium model function pointers cannot be used as arguments to be allocated to registers. 2. the order of allocating registers of this function is the same as that of an ordinary function with the -qf option specified. effect  the object code can be shortened and program execution speed can be improved. usage declare a function with the norec attribute in the function declaration as follows. norec type-name function-name  _ _ leaf can also be described instead of norec .
chapter 11 extended functions user?s manual u15556ej1v0um 321 norec function norec restrictions  no other function can be called from a norec function.  there are restrictions on the type and number of arguments and automatic variables that can be used in a norec function.  when -za is specified, norec is disabled and only _ _leaf is enabled.  the restrictions for arguments and automatic variables are checked during compilation, and an error occurs.  if arguments and automatic variables are declared with a register, the register declaration is ignored.  the following shows the types of arguments and automatic variables that can be used in norec functions.  pointer  char/signed char/unsigned char  int/signed int/unsigned int  short/signed short/unsigned short  long/signed long/unsigned long  float/double/long double (a) restrictions for arguments of function when -zo option is specified  the char type arguments do not perform int extension. table 11-19. restrictions on norec function arguments (when -zo is specified) data type restriction char type max. 8 variables int , short , small model pointer type max. 4 variables large model pointer, long , float , double , long double type max. 2 variables
chapter 11 extended functions user?s manual u15556ej1v0um 322 norec function norec (b) restrictions for arguments of function when -zo option is not specified table 11-20. restrictions on norec function arguments (when -zo is not specified) data type restriction other than pointer max. 14 bytes (max. 6 bytes) note small model pointer, medium model data pointer max. 14 bytes (max. 6 bytes) note medium model function pointer max. 2 variables (cannot be used) note large model pointer max. 3 variables (max. 1 variable) note note the figures enclosed in parentheses indicate values when -qr is not specified. (c) restrictions for automatic variables when -zo option is specified  up to 8 bytes of the automatic variables can be used in the norec function. if there are excess registers used for arguments, they are added to the 8 bytes. automatic variables are allocated to saddr2 area in 1-byte alignment.  in the case that the -qr option is not specified during compilation, if the total size of the arguments and automatic variables exceeds 4 bytes (6 bytes when -ms -qf is specified), an error occurs.
chapter 11 extended functions user?s manual u15556ej1v0um 323 norec function norec (d) restrictions for automatic variables when -zo option is not specified the automatic variables that can be used are allocated to the registers remaining after allocation of arguments, saddr2 area ( _@nrargx ) for the arguments of norec functions, and saddr2 area ( _@nratxx ) for automatic variables of norec functions. table 11-21. restrictions on norec function automatic variables (when -zo is not specified) data type restriction other than pointer max. 22 bytes (max. 6 bytes) note small model pointer, medium model data pointer max. 22 bytes (max. 6 bytes) note medium model function pointer max. 4 variables (cannot be used) note large model pointer max. 6 variables (max. 2 variable) note note the figures enclosed in parentheses indicate values when -qr is not specified. example (c source) norec int rout (int a, int b, int c); int i, j; void main ( ) { int k, l, m; i = l + rout (k, l, m) + ++k ; } norec int rout (int a, int b, int c) { int x, y; return (x + (a<<2)); }
chapter 11 extended functions user?s manual u15556ej1v0um 324 norec function norec (output object of compiler) (with large model, when ?qr option is specified, and -zo option is not specified) extrn saddr2 (_@nrarg0) ; refers to saddr2 area to be used. public _rout public _i public _j public _main @@data dseg _i: ds (2) _j: ds (2) @@code cseg _main: push uup subwg sp, #06h movg whl, sp movg uup, whl movw ax, [up+2] ; stores argument l to register rp2 . movw rp2, ax movw ax, [up] ; stores argument m to register de . movw de, ax movw ax, [up+4] ; stores argument k to register ax . call $!_rout ; calls norec function movw ax, [up+2] ; adds return value of norec function to l . addw bc, ax ; movw ax, [up+4] ; increments k incw ax ; movw [up+4], ax ; addw bc, ax ; assigns the result of addition to i . movw !!_i, bc addwg sp, #06h pop uup ret
chapter 11 extended functions user?s manual u15556ej1v0um 325 norec function norec (output object of compiler?continued) _rout: push uup saves register for arguments. push rp3 ; push vvp ; movw rp3, ax ; assigns the first argument a to rp3 . movw vp, de ; assigns the third argument c to vp . movw up, rp2 ; assigns the second argument b to up . movw ax, rp3 ; receives the first argument a from register rp3 . shlw ax, 2 ; addw ax, _@nrarg0 ; automatic variable x assigned to saddr2 movw bc, ax ; assigns return value to bc register l0004: pop vvp ; restores registers for arguments. pop rp3 pop uup ret end explanation in the above example, the norec attribute is added in the definition of the rout function as well to indicate that the function is norec . compatibility from another c compiler to this c compiler  modification is not required if the keyword norec is not used.  when changing variables to norec variables, modify the program according to the method above. from this c compiler to another c compiler  #define must be used. for details, see 11.6 modifications of c source.
chapter 11 extended functions user?s manual u15556ej1v0um 326 (7) bit type variables bit type variables bit boolean type variables boolean _ _boolean function a bit or boolean type variable is handled as 1-bit data and allocated to saddr2 area.  these variables can be handled the same as an external variable that has no initial value (or has an unknown value).  the c compiler outputs the following bit manipulation instructions for these variables. mov1, and1, or1, xor1, set1, clr1, not1, bt, bf instructions effect  programming at the assembler source level can be performed in c, and the saddr and sfr areas can be accessed in bit units. usage  declare a bit or boolean type inside the module in which the bit or boolean type variable is to be used, as follows.  _ _boolean can also be described instead of bit . bit variable-name boolean variable-name _ _boolean variable-name declare a bit or boolean type inside the module in which the bit or boolean type variable is to be used, as follows. extern bit variable-name extern boolean variable-name extern _ _boolean variable-name  char, int, short , and long type sreg variables (except the elements of arrays and members of structures) and 8-bit sfr variables can be automatically used as bit type variables. variable-name .n (where n = 0 to 31)
chapter 11 extended functions user?s manual u15556ej1v0um 327 bit type variables bit boolean type variables boolean _ _boolean restrictions  an operation on two bit or boolean type variables is performed by using the carry flag. for this reason, the contents of the carry flag between statements are not guaranteed.  arrays cannot be defined or referenced. a bit or boolean type variable cannot be used as a member of a structure or union.  this type of variable cannot be used as the argument type of a function.  the variable cannot be declared with an initial value.  if the variable is described along with a const declaration, the const declaration is ignored.  only operations using 0 and 1 can be performed by the operators and constants shown in the following table.  *, & (pointer reference, address reference), and sizeof operations cannot be performed.  when the -za option is specified, only _ _boolean is enabled. table 11-22. operators that use only constants 0 or 1 (when using bit type variable) classification operator classification operator assignment = bitwise and &, &= bitwise or |, |= bitwise xor ^, ^= logical and && logical or || equal == not equal !=
chapter 11 extended functions user?s manual u15556ej1v0um 328 bit type variables bit boolean type variables boolean _ _boolean table 11-23. number of usable bit type variables condition restrictions (per load module) when -qr option is specified ( saddr2 area [xfd20h to xfddfh]) max. 1536 variables can be used. when -qr option is not specified ( saddr2 area [xfd20h to xfdffh]) max. 1792 variables can be used. the number of usable bit type variables is decreased if sreg variables are used or the -rd and -rs (automatic saddr allocation option) options are specified. example (c source) #define on 1 #define off 0 extern void testb (void); extern void chgb (void); extern bit data1; extern _ _boolean data2; void main () { data1 = on; data2 = off; while (data1) { data1 = data2; testb(); } if (data1 && data2) { chgb(); } }
chapter 11 extended functions user?s manual u15556ej1v0um 329 bit type variables bit boolean type variables boolean _ _boolean (assembler source) indicates the case where the user creates a definition code of a bit type variable. the following example shows the case of the large model (-ml) and the location 0fh (-cs15). in this example, if the compiler output section name @@ bits is used, a link error occurs since the bit segment is changed to the at attribute. therefore, other segment names should be used (if the attribute is saddr2 , the @@bits segment name can be used). public _data1 ; declaration public _data2 bit_seg bseg at 0ffd20h ; allocation to segment _data1 dbit _data2 dbit (output object of compiler) if an extern declaration is not added, the compiler outputs the codes shown below. the following shows the case of the large model. extrn _testb extrn _chgb public _data1 public _data2 public _main @@bits bseg saddr2 _data1 dbit _data2 dbit
chapter 11 extended functions user?s manual u15556ej1v0um 330 bit type variables bit boolean type variables boolean _ _boolean (output object of compiler?continued) @@code cseg _main: set1 _data1 ; initialize by 1 clr1 _data2 ; initialize by 0 l0003: bf _data1, $l0004 ; judgment mov1 cy, _data2 ; assignment mov1 _data1, cy ; assignment call !!_testb br $l0003 l0004: bf _data1, $l0005 ; logical and expression bf _data2, $l0005 ; logical and expression call !!_chgb l0005: l0006: ret end compatibility from another c compiler to this c compiler  modification is not required if the keyword bit , boolean , or _ _boolean is not used.  when changing variables to bit or boolean type variables, modify the program according to the method above. from this c compiler to another c compiler  #define must be used. for details, see 11.6 modifications of c source (as a result of this modification, the bit or boolean type variables are handled as ordinary variables.).
chapter 11 extended functions user?s manual u15556ej1v0um 331 (8) _ _boolean1 type variables _ _boolean1 type variables _ _boolean1 function  _ _boolean1 type variables are handled as 1-bit data and allocated to saddr1 area.  _ _boolean1 type variables are handled in the same manner as external variables without an initial value (undefined).  the compiler outputs the following bit manipulation instructions for these bit variables. mov1, and1, or1, xor1, set1, clr1, not1, bt, bf instructions effect  programming at the assembler source level and bit access to saddr1 area are enabled by c description. usage declares _ _boolean1 type in the module that uses _ _boolean1 type variables. _ _boolean1 variable-name  declares the extern _ _boolean1 in the module that refers to _ _boolean1 type variables. extern _ _boolean1 variable-name  the sreg1 variables (except the element of an array and member of a union) of char/int/short/long types are automatically enabled to be used as _ _boolean1 type variables. variables-name .n (n is 0 to 31)
chapter 11 extended functions user?s manual u15556ej1v0um 332 _ _boolean1 type variables _ _boolean1 restrictions  the operations between _ _boolean1 type variables can be performed using carry flags. therefore, the contents of the carry flag between statements are not guaranteed.  _ _boolean1 type variables cannot define/reference or array.  _ _boolean1 type variables cannot be used as a member of a structure or union.  _ _boolean1 type variables cannot be used as an argument type of a function.  _ _boolean1 type variables cannot be used as a return value of a function.  _ _boolean1 type variables cannot declare with an initial value.  if described with the const declaration, the const declaration is ignored.  only operations using 0 and 1 can be performed by the operators and constants shown in the following table.  *, & (pointer reference, address reference), and sizeof operations cannot be performed. table 11-24. operators that use only constants 0 or 1 (when using bit type variables) classification operator category operator assignment = and in bit units &, &= or in bit units |, | = xor in bit units ^, ^ logical and && logical or || equal == not equal !=
chapter 11 extended functions user?s manual u15556ej1v0um 333 _ _boolean1 type variables _ _boolean1 the following shows the number of usable _ _boolean1 type variables. table 11-25. number of usable _ _boolean1 type variables condition restrictions (per load module) when using saddr1 area [xfe00h to xfe7fh] max. 1024 variables can be used. when sreg1 variables are used, however, the number of usable _ _boolean1 type variables is decreased. example (c source) #define on 1 #define off 0 extern void testb (void); extern void chgb (void); extern _ _boolean1 data1; extern_ _boolean1 data2 ; void main() { data1 = on; data2 = off while (data1) { data1 = data2; testb(); } if (data1 && data2) { chgb(); } }
chapter 11 extended functions user?s manual u15556ej1v0um 334 _ _boolean1 type variables _ _boolean1 (assembler source) indicates the case where the user creates a definition code of a _ _boolean1 type variable. the following example shows the case of the large model (-ml) and the location 0fh (-cs15). in this example, if the compiler output section name @@ bits1 is used, a link error occurs since the bit segment is changed to an at attribute. therefore, other segment names should be used (if the attribute is saddr , the segment name @@bits1 can be used). public _data1 ; declaration public _data2 bit1_seg bseg at 0ffe00h ; allocation to segment _data1 dbit _data2 dbit (output object of compiler) the compiler outputs the following codes if an extern declaration is not added. the following shows the case of the large model. extrn _testb extrn _chgb public _data1 public _data2 public _main @@bits 1 bseg saddr _data1 dbit _data2 dbit
chapter 11 extended functions user?s manual u15556ej1v0um 335 _ _boolean1 type variables _ _boolean1 (output object of compiler?continued) @@code cseg _main : set1 _data1 ; initialize by 1 clr1 _data2 ; initialize by 0 l0003 : bf _data1, $l0004 ; judgment mov1 cy, _data2 ; assignment mov1 _data1, cy ; assignment call !!_testb br $l0003 l0004 : bf _data1, $l0005 ; logical and expression bf _data2, $l0005 ; logical and expression call !!_chgb l0005 : l0006 : ret end compatibility from another c compiler to this c compiler  modification is not required if the keyword _ _boolean1 is not used.  when changing to _ _boolean1 type variables, modify the program according to the method above. from this c compiler to another c compiler  changes are made by #define . for details, refer to 11.6 modifications of c source (by these changes, _ _boolean1 type variables are handled as ordinary variables).
chapter 11 extended functions user?s manual u15556ej1v0um 336 (9) asm statements asm statements #asm, #endasm _ _asm function (a) #asm - #endasm  the assembler source program described by the user can be embedded in an assembler source file to be output by this c compiler by using the preprocessing directives #asm and #endasm .  #asm and #endasm lines will not be output. (b) _ _asm  an assembly instruction is output and inserted in an assembler source by describing an assembly code for a character string literal. effect  global variables of the c source can be manipulated in the assembler source  functions that cannot be described in the c source can be implemented  the assembler source output by the c compiler can be manually optimized and embeded it in the c source (to obtain efficient objects) usage (a) #asm - #endasm  indicate the start of the assembler source with the #asm directive and the end of the assembler source with the #endasm directive. describe the assembler source between #asm and #endasm . #asm : /* assembler source */ #endasm
chapter 11 extended functions user?s manual u15556ej1v0um 337 asm statements #asm, #endasm _ _asm (b) _ _asm use of _ _asm is declared by the #pragma asm specification made at the beginning of the module in which the asm statement is to be described (the uppercase letters and lowercase letters are distinguished for the keywords following #pragma ).  the following items can be described before #pragma asm .  comment  other #pragma directive  preprocessing directive not creating variable definition/reference or function definition/reference  the asm statement is described in the following format in the c source. _ _asm (string literal);  the description method of the character string literal conforms to ansi, and a line can be continued by using an escape character string (\n: line feed, \t: tab) or , or character strings can be linked. restrictions  nesting of #asm directives is not allowed.  if asm statements are used, no object module file will be created. instead, an assembler source file will be created.  only lowercase letters can be described for _ _asm . if _ _asm is described with uppercase and lowercase characters mixed, it is regarded as a user function.  when the -za option is specified, only _ _asm is enabled.  #asm - #endasm and the _ _asm block can only be described inside a function of the c source. therefore, the assembler source is output to cseg (with medium/large model) of the segment name @@code or @@codes cseg base (with small model).
chapter 11 extended functions user?s manual u15556ej1v0um 338 asm statements #asm, #endasm _ _asm example (a) #asm - #endasm (c source) void main ( ) { #asm callt [60h] #endasm } (output object of compiler) the assembler source written by the user is output to the assembler source file. @@code cseg _main: callt [60h] ret end explanation  in the above example, statements between #asm and #endasm will be output as an assembler source program to the assembler source file.
chapter 11 extended functions user?s manual u15556ej1v0um 339 asm statements #asm, #endasm _ _asm (b) _ _asm (c source) #pragma asm int a, b; void main( ) { _ _asm(?\tmovw ax, !_a\t;ax <- a?); _ _asm(?\tmovw !_b, ax\t;b <- ax?); } (assembler source) @@code cseg _main: movw ax, !_a ;ax <- a movw !_b, ax ;b <- ax ret end compatibility  with a c compiler that supports #asm , modify the program according to the format specified by the c compiler.  if the target device is different, modify the assembler source part of the program.
chapter 11 extended functions user?s manual u15556ej1v0um 340 (10) interrupt functions interrupt functions #pragma vect #pragma interrupt function  the address of a described function name is registered to an interrupt vector table corresponding to a specified interrupt request name.  an interrupt function outputs a code to save or restore the following data (except that used in the asm statement) to or from the stack at the beginning and end of the function (after the code if a register bank is specified). (1) registers (2) saddr area for register variables (3) saddr2 area for arguments/ auto variables of norec function (regardless of whether the arguments or variables are used) note, however, that depending on the specification or status of the interrupt function, saving/restoring is performed differently, as follows.  if no change is specified, codes that change the register bank or save/restore register contents, and that save/restore the contents of the saddr2 area are not output regardless of whether the codes are used or not.  if a register bank is specified, a code to select the specified register bank is output at the beginning of the interrupt function, therefore the contents of the registers are not saved or restored.  if ?no change? is not specified and if a function is called in the interrupt function, however, the entire register area is saved or restored, regardless of whether use of registers is specified or not.  if the -qr option is not specified during compilation, the saddr2 area for register variables and the saddr2 area for the arguments/ auto variables of the norec function is not used; therefore, the saved/restored code is not output.  if the size of the saved code is smaller than that of the restored code, the restored code is output.  table 11-26 summarizes the above and shows the save/restore area.
chapter 11 extended functions user?s manual u15556ej1v0um 341 interrupt functions #pragma vect #pragma interrupt table 11-26. save/restore area when interrupt function is used function called function not called without -qr with -qr without -qr with -qr save/restore area no bank stack rbn stack rbn stack rbn stack rbn register used { { all registers { { saddr2 area for register variable used {{ {{ entire saddr2 area for argument/auto variable of norec function {{ stack: use of stack is specified. { : saved rbn: register bank is specified. : not saved caution if there is an asm statement in an interrupt function, and if the area reserved for registers of the compiler is used in that asm statement, the area must be saved by the user. effect  interrupt functions can be described at the c source level.  because the register bank can be changed, codes that save the registers are not output; therefore, object codes can be shortened and program execution speed can be improved.  you do not have to be aware of the addresses of the vector table to recognize an interrupt request name.
chapter 11 extended functions user?s manual u15556ej1v0um 342 interrupt functions #pragma vect #pragma interrupt usage  specify an interrupt request name, a function name, stock switching registers, and whether the saddr2 area is saved/restored, with the #pragma directive. describe the #pragma directive at the beginning of the c source. the #pragma directive is described at the start of the c source (for the interrupt request names, refer to the user?s manual of the target device used). for the software interrupt brk, describe brk_i.  to describe #pragma pc (processor type), describe this #pragma directive after that. the following items can be described before this #pragma directive.  comment statements  preprocessing directive that neither defines nor references a variable or a function #pragma ? vect (or interrupt) ? interrupt request name ? function name ? [stack change specification] ? stack use specification no change specification register bank specification interrupt request name: described in uppercase letters. refer to the user?s manual of the target device used (example: nmi, intp0, etc.). for the software interrupt brk, describe brk_i. function name: name of the function that describes interrupt processing stack change specification: sp = array name [+ offset location] (example: sp = buff + 10) define the array by unsigned char (example: unsigned char buff [10];). stack use specification: stack (default) no change specification: nobank register bank specification: rb0/rb1/rb2/rb3/rb4/rb5/rb6/rb7 ? : space caution the startup routine of this compiler is initialized to register bank 0. therefore, specifying register banks 1 to 7 is necessary.
chapter 11 extended functions user?s manual u15556ej1v0um 343 interrupt functions #pragma vect #pragma interrupt restrictions  an interrupt request name must be described in uppercase letters.  a duplication check on interrupt request names will be made within only one module.  if the same or another interrupt occurs because of the contents of the priority specification flag register and interrupt mask flag register while a vectored interrupt is being processed, the contents of the registers may be changed if a register bank is specified or no change is specified, resulting in an error. the compiler, however, cannot check this error.  callt/callf/noauto/norec/_ _callt/_ _callf/_ _leaf/_ _rtos_interrupt/_ _pascal/_ _flash cannot be specified as the interrupt function.  an interrupt function is specified with void type (example: void func ( void );) because it cannot have an argument or a return value.  even if an asm statement exists in the interrupt function, codes saving all the registers and variable areas are not output. if an area reserved for the compiler is used in the asm statement in the interrupt function, therefore, or if a function is called in the asm statement, the user must save the registers and variable areas on their own responsibility.  if a function specifying no change, register bank, or stack change as the saving destination via a #pragma vect/#pragma interrupt specification is not defined in the same module, a warning message is output and the stack change is ignored. in this case, the default stack is used.  when stack change is specified, the stack pointer is changed to the location where offset is added to the array name symbol. the area of the array name is not secured by the #pragma directive. it needs to be defined separately as a global unsigned char type array.  the code that changes the stack pointer is generated at the start of a function, and the code that sets the stack pointer back is generated at the end of a function.  when the keywords sreg/_ _sreg are added to the array for stack change, it is regarded that two or more variables with the different attributes and the same name are defined, and a compilation error occurs. it is possible to allocate an array in saddr area using the -rd option, but code and speed efficiency will not be improved because the array is used as a stack. it is recommended to use the saddr area for purposes other than a stack.  a stack change cannot be specified simultaneously with ?no change?. if specified so, an error occurs.  the stack change must be described before the stack use/register bank specification. if the stack change is described after the stack use/register bank specification, an error occurs.
chapter 11 extended functions user?s manual u15556ej1v0um 344 interrupt functions #pragma vect #pragma interrupt example (c source 1) #pragma interrupt nmi inter rb1 void inter() { /* interrupt handling to nmi pin input */ } (output object of compiler) @@base cseg base _inter: register bank switching code save code of saddr area for use by c compiler interrupt handling to nmi input (function body) restore code of saddr area for use by c compiler reti @@vect02 cseg at 02h ; nmi dw _inter (c source 2) (when stack change and register bank are specified) #pragma interrupt intp0 inter sp=buff+10 rb2 unsigned char buff[10]; void func(); void inter(); { func(); }
chapter 11 extended functions user?s manual u15556ej1v0um 345 interrupt functions #pragma vect #pragma interrupt (output object of compiler) with large model @@base cseg base _inter: sel rb2 ; changes register bank push whl ; movg whl,sp ; changes stack pointer movg sp,#_buff+10 ; push whl ; call !!_func pop whl ; movg sp,whl ; sets back stack pointer pop whl ; reti @@vect06 cseg at 0006h dw _inter compatibility from another c compiler to this c compiler  modification is not required if interrupt functions are not used at all.  when changing an ordinary function to an interrupt function, modify the program according to the method above. from this c compiler to another c compiler  an interrupt function can be used as an ordinary function by deleting its specification with the #pragma vect , #pragma interrupt directive.  when an ordinary function is to be used as an interrupt function, change the program according to the specifications of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 346 (11) interrupt function qualifier (_ _interrupt, _ _interrupt_brk) interrupt function qualifier _ _interrupt _ _interrupt_brk function  a function declared with the _ _interrupt qualifier is regarded as a hardware interrupt function, and execution is returned by the return reti instruction for non-maskable/maskable interrupt functions.  by declaring a function with the _ _interrupt_brk qualifier, the function is regarded as a software interrupt function, and execution is returned by the return instruction retb for software interrupt functions.  a function declared with this qualifier is regarded as a (non-maskable/maskable/software) interrupt function, and saves or restores the registers and variable areas (1) and (3) below, which are used as the work area of the compiler, to or from the stack. if a function call is described in this function, however, all the variable areas are saved to the stack. (1) registers (2) saddr area for register variables (3) saddr area for arguments/ auto variables of norec function (regardless of usage) remark if the -qr option is not specified (default) during compilation, codes to save or restore areas (2) and (3) are not output because these areas are not used. effect  by declaring a function with this qualifier, the setting of a vector table and interrupt function definition can be described in separate files.
chapter 11 extended functions user?s manual u15556ej1v0um 347 interrupt function qualifier _ _interrupt _ _interrupt_brk usage  describe either _ _interrupt or _ _interrupt_brk as the qualifier of an interrupt function. (for non-maskable/maskable interrupt function) _ _interrupt void func() { processing } (for software interrupt function) _ _interrupt_brk void func() { processing } restrictions  callt/callf/noauto/norec/_ _callt/_ _callf/_ _leaf/_ _rtos_interrupt/_ _pascal/_ _flash cannot be specified for the interrupt function . cautions  the vector address is not set by merely declaring this qualifier. the vector address must be separately set by using the #pragma vect/interrupt directive or assembler description.  the saddr area and registers are saved to the stack.  even if the vector address is set or the saving destination is changed by #pragma vect (or interrupt ) ..., the change in the saving destination is ignored if there is no function definition in the same file, and the default stack is assumed.  to define an interrupt function in the same file as the #pragma vect (or interrupt ) ... specification, the function name specified by #pragma vect (or interrupt ) ... is judged as the interrupt function, even if this qualifier is not described (for details of #pragma vect/interrupt , refer to 11.5 (10) interrupt functions ).
chapter 11 extended functions user?s manual u15556ej1v0um 348 interrupt function qualifier _ _interrupt _ _interrupt_brk example declare or define interrupt functions in the following format. the code to set the vector address is generated by #pragma interrupt . #pragma interrupt intp0 inter rb1 #pragma interrupt brk_i inter_b rb2 /* note */ _ _interrupt void inter( ); /* prototype declaration */ _ _interrupt_brk void inter_b( ); /* prototype declaration */ _ _interrupt void inter( ) {processing}; /* function body */ _ _interrupt_brk void inter_b( ) {processing}; /* function body */ note the interrupt request name of the software interrupt is ?brk_i.? compatibility from another c compiler to this c compiler  modification is not required unless interrupt functions are supported.  modify the interrupt functions, if necessary, according to the method above. from this c compiler to another c compiler  #define must be used to allow the interrupt qualifiers to be handled as ordinary functions.  to use the interrupt qualifiers as interrupt functions, modify the program according to the specifications of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 349 (12) interrupt functions interrupt functions #pragma di #pragma ei functions  codes di and ei are output to an object and an object file is created.  if the #pragma directive is missing, di( ) and ei( ) are regarded as ordinary functions.  if ? di( ) ;? is described at the beginning in a function (except for the declaration of an automatic variable, a comment, or a preprocessing directive), the di code is output before the preprocessing of the function (immediately after the label of the function name).  to output the code of di after the preprocessing of the function, open a new block before describing ? di( ) ;? (delimit this block with ?{?).  if ? ei( ) ;? is described at the end of a function (except for comments and preprocessing directives), the ei code is output after the postprocessing of the function (immediately before the code ret ).  to output the ei code before the postprocessing of a function, close a new block after describing ? ei( ) ;? (delimit this block with ?}?). effect  a function disabling interrupts can be created. usage  describe the #pragma di and #pragma ei directives at the beginning of the c source. however, the following statement and directives may precede the #pragma di and #pragma ei directives.  comment statement  other #pragma directives  preprocessing directive that neither defines nor references a variable or function  describe di( ) ; or ei( ) ; in the source in the same manner as a function call.  di and ei can be described in either uppercase or lowercase letters after #pragma .
chapter 11 extended functions user?s manual u15556ej1v0um 350 interrupt functions #pragma di #pragma ei restrictions  when using these interrupt functions, di and ei cannot be used as function names.  di and ei must be described in uppercase letters. if described in lowercase letters, they will be handled as ordinary functions. example (c source 1) #pragma di #pragma ei void main () { di (); function body ei (); } (output object of compiler) _main: di preprocessing function body postprocessing ei ret
chapter 11 extended functions user?s manual u15556ej1v0um 351 interrupt functions #pragma di #pragma ei (c source 2) #pragma di #pragma ei void main () { { di(); function body ei(); } } (output object of compiler) _main: preprocessing di function body ei postprocessing ret compatibility from another c compiler to this c compiler  modification is not required if interrupt functions are not used at all.  when changing an ordinary function to an interrupt function, modify the program according to the method above. from this c compiler to another c compiler delete the #pragma di and #pragma ei directives or invalidate these directives by separating them with #ifdef . di and ei can be used as ordinary function names (example: #ifdef_ _k4_ _ ... #endif ). when an ordinary function is to be used as an interrupt function, modify the program according to the specifications of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 352 (13) cpu control instruction cpu control instructions #pragma halt/stop/brk/nop function  the following codes are output to an object to create an object file. (1) instruction for halt operation note 1 (2) instruction for stop operation note 2 (3) brk instruction (4) nop instruction notes 1. the setting of stop mode and selection of the internal system clock is possible using the stbc register. the c compiler reads stbc, checks the ck1/ck0 value of the internal system clock selection, and accordingly outputs the instruction to set the value for halt to stbc. 2. the c compiler reads stbc, checks the ck1/ck0 value of the internal system clock selection, and accordingly outputs the instruction to set the value for stop to stbc. effect  the standby function of a microcontroller can be used with a c program.  a software interrupt can be generated.  the clock can continue without the cpu operating. usage  describe the #pragma halt , #pragma stop , #pragma nop , and #pragma brk instructions at the beginning of the c source.  the following items can be described before the #pragma directive.  comment statement  other #pragma directive  preprocessing directive that neither defines nor references a variable or function  the keywords following #pragma can be described in either uppercase or lowercase letters.
chapter 11 extended functions user?s manual u15556ej1v0um 353 cpu control instructions #pragma halt/stop/brk/nop  describe as follows in uppercase letters in the c source in the same format as a function call. (1) halt(); (2) stop(); (3) brk(); (4) nop(); restrictions  when this feature is used, halt( ), stop( ), brk( ), and nop( ) cannot be used as function names.  describe halt, stop, brk, and nop in uppercase letters. if they are described in lowercase letters, they are handled as ordinary functions. example (c source) #pragma halt #pragma stop #pragma brk #pragma nop void main() { halt(); stop(); brk(); nop(); }
chapter 11 extended functions user?s manual u15556ej1v0um 354 cpu control instructions #pragma halt/stop/brk/nop (output object of compiler) with large model @@code cseg _main : ; line 7 : halt(); mov a,stbc bt a,4,$$+12 bt a.5,$$+24 mov stbc,#01h br $$+21 bt a.5,$$+9 mov stbc,#011h br $$+12 mov stbc,#031h br $$+6 mov stbc,#021h ; line 8 : stop() ; mov a,stbc bt a.4,$$+12 bt a.5,$$+24 mov stbc,#02h br $$+21 bt a.5,$$+9 mov stbc,#012h br $$+12 mov stbc,#032h br $$+6 mov stbc,#022h ; line 9 : brk() ; brk ; line 10 : nop() ; nop ret
chapter 11 extended functions user?s manual u15556ej1v0um 355 cpu control instructions #pragma halt/stop/brk/nop compatibility from another c compiler to this c compiler  modification is not required if the cpu control instructions are not used.  when the cpu control instructions are used, modify the program according to the method above. from this c compiler to another c compiler ? if ? #pragma halt ?, ? #pragma stop ?, ? #pragma brk ?, and ? #pragma nop ? statements are deleted or delimited with #ifdef , halt , stop , brk , and nop can be used as function names.  to use these instructions as cpu control instructions, modify the program according to the specifications of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 356 (14) callf functions callf functions callf/_ _callf function  the callf instruction stores the body of a function in the callf area. this makes code shorter than ordinary call instructions.  if a function stored in the callf area is to be referenced without a prototype declaration, the function must be called by an ordinary call instruction.  the callee (the function to be called) is the same as an ordinary function. effect  the object code can be shortened. usage  add the callf attribute or _ _callf attribute to the beginning of a function at the time of the function declaration as follows. callf extern type-name function-name _ _callf extern type-name function-name restrictions  functions declared with callf will be located in the callf entry area. at which address in the area each function is to be located will be determined at the time of linking object modules. for this reason, when using any callf function in an assembler source module, the routine to be created must be made ?relocatable? using symbols.  a check on the number of callf functions is made at linking time.  callf entry area: 800h to fffh  the number of functions that can be declared with the callf attribute is not limited.  the total number of functions with the callf attribute is not limited as long as the first function is within the range of [800h to fffh].  when the -za option is specified, only _ _callf is enabled.
chapter 11 extended functions user?s manual u15556ej1v0um 357 callf functions callf/_ _callf example (c source 1) (c source 2) (output object of compiler) with large model extrn _fsub ; declaration callf !_fsub ; call (to be allocate to callf entry area) public _fsub ; declaration @@calf cseg fixed _fsub: ; function definition : function body : compatibility from another c compiler to this c compiler  modification is not required if the keyword callf/_ _callf is not used.  when changing functions to callf functions, modify the program according to the method above. from this c compiler to another c compiler  #define must be used to allow callf functions to be handled as ordinary functions. _ _callf extern int fsub(); void main () { int ret_val; ret_val = fsub(); } _ _callf int fsub() { int val; return val; }
chapter 11 extended functions user?s manual u15556ej1v0um 358 (15) 16 mb expansion space utilization 16 mb expansion space utilization 16 mb expansion -ml function  an object file that can linearly access a 16 mb expansion space is created. effect  the 16 mb expansion space can be accessed in the same manner as 16-bit addressing (64 kb) mode. usage  specify the -ml option (default) during compilation. restrictions  when the -ms option is specified at the time of startup: small model: combined code/data block capacity of 16 kb  when the -mm option is specified at the time of startup: medium model: capacity of up to 1 mb for the code block and 16 kb for the data block  when the -ml option is specified at the time of startup: large model: combined code/data block capacity of 16 mb, including up to 1 mb for the code block and 16 mb for the data block. example (c source) sreg int *ladr; int *grob; void main ( ) { int atval; *ladr = atval; *grob = atval; }
chapter 11 extended functions user?s manual u15556ej1v0um 359 16 mb expansion space utilization 16 mb expansion -ml (output object of compiler) with small model @@codes cseg base _main : push rp3 ; preprocessing of function movw ax,rp3 movw [_ladr],ax ; *ladr = atval movw hl,!_grob movw ax,rp3 movw [hl],ax ; *grob = atval pop rp3 ; postprocessing of function ret with medium model @@code cseg _main: push rp3 ; preprocessing of function movw de,_ladr movw ax,rp3 movw [de],ax ; *ladr = atval movw de,!!_grob movw ax,rp3 movw [de],ax ; *grob = atval pop rp3 ; postprocessing of function ret
chapter 11 extended functions user?s manual u15556ej1v0um 360 16 mb expansion space utilization 16 mb expansion -ml (output object of compiler) with large model @@code cseg _main : push rp3 ; preprocessing of function movw ax,rp3 movw [%_ladr],ax ; *ladr = atval movg whl,!!_grob movw [hl],ax ; *grob = atval pop rp3 ; postprocessing of function ret compatibility from another c compiler to this c compiler  modification is not required if it has been re-compiled with the -ml option added during compilation, when the 16 mb expansion space is to be used. from this c compiler to another c compiler  the source program need not be modified if it is re-compiled with each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 361 (16) allocation function allocation function allocation function -cs function  with the medium model (when the -mm option is specified) or with the large model (when the -ml option is specified), the allocation of the saddr area can be changed by using the -cs option. effect  when the -cs15 option is specified, the code space can be continuously used. usage  the -cs option is specified during compilation. the -cs option performs the following operation. -cs0 : allocates saddr area to 0fd20h to 0ffffh -cs15/-cs0fh : allocates saddr area to 0ffd20h to 0fffffh -csa : does not check with compiler but with linker restrictions  use the startup routine included with to this compiler that specifies the location specified by the -cs option. the location instruction is described in the startup routine (for details of the startup routine, refer to the cc78k4 c compiler operation user?s manual (u15557e) ). example (c source) void main ( ) { /* function body */ } (output object of compiler) with large model ( -ml ) and location 0 ( -cs0 ) specified $chgsfr (0) $processor(4026) ; variable declaration etc. @@code cseg _main: ; function preprocessing ; function body processing ; function postprocessing ret
chapter 11 extended functions user?s manual u15556ej1v0um 362 allocation function allocation function -cs with large model ( -ml ) and location 15 ( -cs15 ) specified $chgsfr (15) $processor 4026) ; variable declaration etc. @@code cseg _main: ; function preprocessing ; function body processing ; function postprocessing ret with large model ( -ml ) and without compile check ( -csa ) specified $chgsfra $processor(4026) ; variable declaration etc. @@code cseg _main: ; function preprocessing ; function body processing ; function postprocessing ret compatibility from another c compiler to this c compiler  when using the medium model or large model, modification is not required if the location position is specified by the -cs option during compilation and the source program is re-compiled. from this c compiler to another c compiler  the source program need not be modified if it is re-compiled with each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 363 (17) absolute address access function absolute address access function #pragma access function  a code to access the ordinary ram space is output to the object with direct inline expansion, not by function call, and an object file is created.  if the #pragma directive is not described, a function accessing an absolute address is regarded as an ordinary function. effect  a specific address in the ordinary memory space can be easily accessed using c description. usage  describe the #pragma access directive at the beginning of the c source.  describe the directive in the source in the same format as a function call.  the following items can be described before #pragma access . . comment statement . other #pragma directives . preprocessing directive that neither defines nor references a variable or function  the keywords following #pragma can be described in either uppercase or lowercase letters. the following four function names are available for absolute address accessing. peekb, peekw, pokeb, pokew
chapter 11 extended functions user?s manual u15556ej1v0um 364 absolute address access function #pragma access [list of functions for absolute address accessing] (a) unsigned char peekb (addr); unsigned int addr; (small model) unsigned long addr; (medium model/large model) returns 1-byte contents of address addr . (b) unsigned int peekw (addr); unsigned int addr; (small model) unsigned long addr; (medium model/large model) returns 2-byte contents of address addr . (c) void pokeb (addr, data); unsigned int addr; (small model) unsigned long addr; (medium model/large model) unsigned char data; writes 1-byte contents of data to the position indicated by address addr . (d) void pokew (addr, data); unsigned int addr; (small model) unsigned long addr; (medium model/large model) unsigned int data; writes 2-byte contents of data to the position indicated by address addr . restrictions  a function name for absolute address accessing must not be used.  describe functions for absolute address accessing in lowercase letters. functions described in uppercase letters are handled as ordinary functions.
chapter 11 extended functions user?s manual u15556ej1v0um 365 absolute address access function #pragma access example (c source) #pragma access char a; int b; void main () { a = peekb(0x1234); a = peekb(0xfe23); b = peekw(0x1256); b = peekw(0xfe68); pokeb(0x1234, 5); pokeb(0xfe23, 5); pokew(0x1256, 100); pokew(0xfe68, 100); }
chapter 11 extended functions user?s manual u15556ej1v0um 366 absolute address access function #pragma access (output object of compiler) with large model @@code cseg -main: mov a, !01234h mov !!_a,a mov a, !0fe23h mov !!_a,a movw ax, !01256h movw !!_b,ax movw ax, 0fe68h movw !!_b,ax mov a, #05h ;5 mov !01234h,a mov !0fe23h,a movw ax, #064h ;100 movw !01256h,ax movw !0fe68h,ax ret compatibility from another c compiler to this c compiler  modification is not required if a function for absolute address accessing is not used.  modify the program according to the method above if a function for absolute address accessing is used. from this compiler to another c compiler  delete the ? #pragma access ? statement or delimit with #ifdef . the function name of absolute address accessing can be used as a function name.  when using a function for absolute address accessing, modify the program according to the specifications of each compiler ( #asm , #endasm , asm( ); ,etc.)
chapter 11 extended functions user?s manual u15556ej1v0um 367 (18) bit field declaration bit field declaration bit field declaration (1) extension of type specifier function  the bit field of unsigned char type is not allocated straddling over a byte boundary.  the bit field of unsigned int type is not allocated straddling over a word boundary, but can be allocated straddling over a byte boundary.  the bit fields of the same type are allocated in the same byte units (or word units). if the types are different, the bit fields are allocated in different byte units (or word units). effect  the memory can be saved, the object code can be shortened, and the execution speed can be improved. usage  as a bit field type specified, unsigned char type can be specified in addition to unsigned int type. declare as follows. struct tag-name { unsigned char field-name: bit-width ; unsigned char field-name: bit-width ; : unsigned int field-name: bit-width ; }; example struct tagname { unsigned char a:1; unsigned char b:1; : unsigned int c:2; unsigned int d:1;
chapter 11 extended functions user?s manual u15556ej1v0um 368 bit field declaration bit field declaration compatibility from another c compiler to this c compiler  modification is not required.  change the type specifier to use unsigned char as the type specifier. from this c compiler to another c compiler  modification is not required if unsigned char is not used as a type specifier.  change unsigned char , if it is used as a type specifier, to unsigned int . (2) allocation direction of bit field function  the direction in which a bit field is to be allocated is changed and the bit field is allocated from the msb side when the -rb option is specified.  if the -rb option is not specified, the bit field is allocated from the lsb side. usage  the -rb option is specified during compilation to allocate the bit field from the msb side.  do not specify the option to allocate the bit field from the lsb side. example 1 (bit field declaration) struct t { unsigned char a:1; unsigned char b:1; unsigned char c:1; unsigned char d:1; unsigned char e:1; unsigned char f:1; unsigned char g:1; unsigned char h:1; };
chapter 11 extended functions user?s manual u15556ej1v0um 369 bit field declaration bit field declaration explanation because a through h are 8 bits or less, they are allocated in 1-byte units. if the bit field is allocated to saddr2 or saddr1 area by the keywords sreg/_ _sreg/_ _sreg1 , a bit manipulation instruction is output, and codes can be reduced. figure 11-1. bit allocation by bit field declaration (example 1) bit allocation from msb with -rb option specified bit allocation from lsb without -rb option specified msb lsb msb lsb abcde f gh hg f edcba example 2 (bit field declaration) struct t { char a; unsigned char b:2; unsigned char c:3; unsigned char d:4; int e; unsigned int f:5; unsigned int g:6; unsigned char h:2; unsigned int i:2; }; explanation if the bit field is allocated to saddr2 or saddr1 area by the keywords sreg/_ _sreg/_ _sreg1 , the code efficiency can be improved.
chapter 11 extended functions user?s manual u15556ej1v0um 370 bit field declaration bit field declaration figure 11-2. bit allocation by bit field declaration (example 2) (1/2) bit allocation from msb with -rb option specified bit allocation from lsb without -rb option specified msb lsb b c vacant a 10 member a of char type is allocated to the first byte unit. b and c are allocated from the next byte unit. if the vacancy has run short, the members are allocated to the next byte unit. because the vacancy is 3 bits and d is 4 bits in this example, d is allocated to the next byte unit. e d vacant 32 g vacant e 54 the 78k/iv series has 1-byte alignment; therefore, e (2 bytes) can straddle over a byte boundary. h vacant f g 76 because g is an unsigned int type bit field, it can be allocated across byte boundary. h is an unsigned char type bit field; it is therefore allocated to the next byte unit, instead to the same byte unit as g , which is an unsigned int type bit field. h vacant vacant 98 i is an unsigned int type bit field and can be allocated to the next word unit. remark the numbers below the allocation diagrams indicate the byte offset values from the beginning of the structure. msb lsb vacant c b a 10 e vacant d 32 gf e 54 vacant h vacant g 76 vacant vacant i 98
chapter 11 extended functions user?s manual u15556ej1v0um 371 bit field declaration bit field declaration when the -ra option or -rp option is specified, the bit field is made 2-byte alignment. the location of the bit field above is as follows. figure 11-2. bit allocation by bit field declaration (example 2) (2/2) bit allocation from msb with -rb option specified bit allocation from lsb without -rb option specified msb lsb b c vacant a 10 e d vacant 32 ee 54 f g g vacant 76 vacant h vacant 98 i vacant vacant 11 10 msb lsb vacant c b a 10 vacant vacant d 32 ee 54 vacant g g f 76 vacant vacant h 98 vacant vacant i 11 10
chapter 11 extended functions user?s manual u15556ej1v0um 372 bit field declaration bit field declaration example 3 (bit field declaration) struct char a; unsigned int b:6; unsigned int c:7; unsigned int d:4; unsigned char e:3; unsigned int f:10; unsigned int g:2; unsigned int h:5; unsigned int i:6; }; figure 11-3. bit allocation by bit field declaration (example 3) (1/2) since b and c are bit fields of type unsigned int , they are allocated from the next word unit. since d is also a bit field of type unsigned int , it is allocated from the next word unit. vacant d e vacant e vacant vacant since e is a bit field of type unsigned char , it is allocated to the next byte unit. msb lsb c vacant b c vacant a cb a d vacant vacant c msb lsb bit allocation from msb with ? rb option specified bit allocation from lsb without ? rb option specified
chapter 11 extended functions user ? s manual u15556ej1v0um 373 bit field declaration bit field declaration figure 11-3. bit allocation by bit field declaration (example 3) (2/2) f and g , and h and i are each allocated to separate word units. when the ? ra option or ? rp option is specified, the bit field is made 2-byte alignment. the location of the bit field above is as follows. a vacant a vacant c vacant vacant d vacant d d vacant vacant e vacant vacant c b c vacant e vacant vacant f f g vacant vacant g f f ii h h i i vacant vacant remark the numbers below the allocation diagrams indicate the byte offset values from the beginning of the structure. f f g vacant h f f g vacant i i vacant vacant i i h msb lsb msb lsb bit allocation from msb with ? rb option specified bit allocation from lsb without ? rb option specified
chapter 11 extended functions user ? s manual u15556ej1v0um 374 bit field declaration bit field declaration compatibility from another c compiler to this c compiler  modification is not required. from this c compiler to another c compiler  modification is required if the -rb option is used and coding is performed taking the bit field allocation sequence into consideration.
chapter 11 extended functions user ? s manual u15556ej1v0um 375 (19) changing compiler output section name #pragma section ? #pragma section function  a compiler output section name is changed and a start address is specified. if the start address is omitted, the default allocation is assumed. for the compiler output section name and default location, refer to appendix b list of segment names . in addition, the location of sections can be specified by omitting the start address and using the link directive file at the time of link. for the link directives, refer to the ra78k4 assembler package operation user ? s manual .  to change section names @@calt and @@calf with an at start address specified, the callt and callf functions must be described before or after the other functions in the source file.  if data is described after the #pragma directive is described, that data is located in the data change section. another change instruction is possible, and if data is described after the rechange instruction, that data is located in the rechange section. if data defined before a change is redefined after the change, it is located in the rechanged section. note that this is valid in the same way for static variables (within the function). effect  changing the compiler output section repeatedly in one file enables location of each section independently, so that data can be located independently in the desired data unit. usage  specify the name of the section to be changed, a new section name, and the start address of the section, by using the #pragma directive as indicated below. describe this #pragma directive at the beginning of the c source. describe this #pragma directive after #pragma pc (processor type). the following items can be described before this #pragma directive.  comment statement  preprocessing directive that neither defines nor references a variable or a function however, all the sections in bseg and dseg , and the @@cnst , @@cnsts and @@cnstm sections in cseg can be described anywhere in the c source, and rechange instructions can be performed repeatedly. to return to the original section name, describe the compiler output section name in the changed section name. declare as follows at the beginning of the file. #pragma section compiler-output-section-name new section-name [at start address]  of the keywords to be described after #pragma , be sure to describe the compiler output section name in uppercase letters. section, at can be described in either uppercase or lowercase letters, or in a combination of these.
chapter 11 extended functions user ? s manual u15556ej1v0um 376 #pragma section ? #pragma section  the format in which the new section name is to be described conforms to the assembler specifications (up to eight letters can be used for a segment name).  only the hexadecimal numbers of the c language and the hexadecimal numbers of the assembler can be described as the start address. [hexadecimal numbers of c language] 0xn / 0xn...n 0xn / 0xn...n (n = 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f) [hexadecimal numbers of assembler] nh/n...nh nh/n...nh (n = 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f)  the hexadecimal number must start with a numeral. example to express a numeric value with a value of 255 in hexadecimal numbers, specify zero before f. it is therefore 0ffh.  when the -qr option is not specified, the start address specification is within the following range. 0xfe2c to 0xfe7f  for sections other than the @@cnst , @@cnsts and @@cnstm sections in cseg , that is, sections which locate functions, this #pragma directive cannot be described at other than the beginning of the c source (after the c text is described). if described, it causes an error.  if this #pragma directive is executed after the c text is described, an assembler source file is created without an object module file being created.  if this #pragma directive is described after the c text is described, a file that contains this #pragma directive and that does not have the c text (including external reference declarations for variables and functions) cannot be included. this results in an error (refer to error description example 1 ).  an #include statement cannot be described in a file that executes this #pragma directive following the c text description. if described, it causes an error (refer to error description example 2 ).  if #include statement follows the c text, this #pragma directive cannot be described after this description. if described, it causes an error. (refer to error description example 3 ).
chapter 11 extended functions user ? s manual u15556ej1v0um 377 #pragma section ? #pragma section example 1 section name @@code is changed to cc1 and address 2400h is specified as the start address. (c source) #pragma section @@code cc1 at 2400h void main() { function body } (output object) cc1 cseg at 2400h _main: preprocessing function body postprocessing ret example 2 this example shows a description in which this #pragma directive is described following the c text. the statement beginning with the double slashes (//) indicates the section to be located. #pragma section @@data ??data int a1; // ??data _sreg int b1; // @@dats int c1 = 1; // @@init and @@r_init const int d1 = 1; // @@cnst #pragma section @@dats ??dats int a2; // ??data _sreg int b2; // ??dats int c2 = 2; // @@init and @@r_init const int d2 = 2; // @@cnst #pragma section @@data ??data2 // ??data is closed automatically and ??data2 becomes valid. int a3; // ??data2 _sreg int b3; // ??dats int c3 = 3; // @@init and @@r_init const int d3 = 3; // @@cnst
chapter 11 extended functions user ? s manual u15556ej1v0um 378 #pragma section ? #pragma section #pragma section @@data @@data // ??data2 is closed and processing returns to the default // @@data. #pragma section @@init ??init #pragma section @@r_init ??r_init //if both names @@init and @@r_init are not changed, // romization becomes invalid. int a4; // @@data _sreg int b4; // ??dats int c4 = 4; // ??init and ??r_init const int d4 = 4; // @@cnst #pragma section @@init @@init #pragma section @@r_init @@r_init // ??init and ??r_init are closed and return to the defaults #pragma section @@bits ??bits _boolean e4; // ??bits #pragma section @@cnst ??cnst char*const p = ?hello?; // both p and ? hello ? ??cnst example 3 #pragma section @@init ??init1 #pragma section @@r_init ??r_int1 #pragma section @@data ??data1 char c1; int i2; #pragma section @@init ??init2 #pragma section @@r_init ??r_int2 #pragma section @@data ??data2 char c1; int i2 = 1; #pragma section @@data ??data3 #pragma section @@init ??init3 #pragma section @@r_init ??r_int3 extern char c1; // ??data3 int i2; // ??init3 and ??r_int3 #pragma section @@data ??data4 #pragma section @@init ??init4 #pragma section @@r_init ??r_int4
chapter 11 extended functions user ? s manual u15556ej1v0um 379 #pragma section ? #pragma section example 4 (method to specify the location of a section by link directives) 1. change the section name whose location is to be changed in the c source. (in this example, @@data is changed to dat1 , and @@init is changed to dat2 ) (c source) #pragma section @@data dat1 #pragma section @@init dat2 unsigned int d1,d2,d3; unsigned long l1, l2; unsigned int i =1; : (output object of compiler) @@r_int cseg ; dw 01h ;1 dat2 dseg _i : ds (2) dat1 dseg _d1 : ds (2) _d2 : ds (2) _d3: ds (2) _l1 : ds (4) _l2 : ds (4) 2. create a link directive file. (link directive file lk78k4.job) memory extram1:(0f0000h , 01000h) memory extram2:(0f1000h , 01000h) : merge dat1 : = extram1 merge dat2 : at(0f1000h) = extram2
chapter 11 extended functions user ? s manual u15556ej1v0um 380 #pragma section ? #pragma section 3. link by specifying the link directive file using the linker option -d . > lk78k4 s4.rel sample.rel -bcl4.lib -dlk78k4.job -s the following example explains the restrictions on describing this #pragma directive following the c text. error description example 1 a1.h #pragma section @@data ??data1 // file with a #pragma section only. a2.h extern int func1 (void); #pragma section @@data ??data2 // file where there is c text and this #pragma directive follows // after. a3.h #pragma section @@data ??data3 // file with a #pragma section only. a4.h #pragma section @@data ??data3 extern int func2 (void); // file that includes c text. a.c #include ?a1.h? #include ?a2.h? #include ?a3.h? // error. // there is c text in a2.h and after that this #pragma directive is // included, so the file that includes this #pragma directive only, // a3.h, cannot be included. #include ?a4.h?
chapter 11 extended functions user ? s manual u15556ej1v0um 381 #pragma section ? #pragma section error description example 2 b1.h const int i; b2.h const int j; #include ?b1.h? // there is c text and there is no file (b.c) where this #pragma // directive is executed after it, so there is no error. b.c const int k; #pragma section @@data ??data1 #include ?b2.h? // error. // there is c text, and in the file following it where this #pragma // directive is executed (b.c), a subsequent #include statement // cannot be described. error description example 3 c1.h extern int j; #pragma section @@data ??data1 // this #pragma directive is included and processed before c3.h processing, so there is // no error. c2.h extern int k; #pragma section @@data ??data2 // error. // there is c text in c3.h and after that there is an #include // statement, so this #pragma directive cannot be included after // that. c3.h #include ?c1.h? extern int i; #include ?c2.h? #pragma section @@data ??data3 // error. // there is c text, and after that there is an #include statement, so // this #pragma directive cannot be included after that.
chapter 11 extended functions user ? s manual u15556ej1v0um 382 #pragma section ? #pragma section c.c #include ?c3.h? #pragma section @@data??data4 // error. // there is c text in c3.h and after that there is an #include // statement, so this #pragma directive cannot be included after // that. int i; compatibility from another c compiler to this c compiler  modification is not required if the section name change function is not supported.  when changing the section name, modify the program according to the method above. from this c compiler to another c compiler  delete #pragma section ... or delimit it with #ifdef .  when changing the section name, modify the program according to the specifications of each compiler. restrictions  a section name that indicates a segment for the vector table (e.g., @@vect02 ) must not be changed.  if two or more sections with the same name as the one specifying the at start address exist in another file, a link error occurs.  when changing compiler output section names @@dats , @@bits , and @@inis , limit the range of the specified address within saddr2 area. ( saddr2 area) 0xfd20 to 0xfdff (with the small model, or when -cs0 of the medium model/large model is specified) 0xffd20 to 0xffdff (when -cs15 of the medium model/large model is specified or default)  when changing compiler output section names @@dats1 , @@bits1 , and @@inis1 , limit the range of the specified address within saddr1 area. ( saddr1 area) 0xfe00 to 0xfeff (with the small model, or when -cs0 of the medium model/large model is specified) 0xffe00 to 0xffeff (when -cs15 of the medium model/large model is specified or default) remark of the areas shown above, 0xxfe80 to 0xxfeff (when -cs0 is specified: x = 0, when -cs15 is specified: x = f) are areas for registers. care must be taken when specifying these areas.  when the -csa option is specified, the following addresses cannot be specified for the start address specification. 0xfd00 to 0xfeff, 0xffd00 to 0xffeff
chapter 11 extended functions user?s manual u15556ej1v0um 383 #pragma section? #pragma section? caution  a section is equivalent to a segment of the assembler.  the compiler does not check whether the new section name is duplicated with another symbol. therefore, the user must check that the section name is not duplicated by assembling the output assemble list.  if a section name (*) related to romization is changed by using #pragma section , the startup routine must be changed by the user on his/her own responsibility. (*) romization-related section name @@r_init, @@r_inis, @@rsinit, @@rsinis @@init, @@inis, @@rsins1, @@r_ins1, @@inis1 the startup routine to be used when a section related to romization is changed, and an example of changing the end module are described below.
chapter 11 extended functions user?s manual u15556ej1v0um 384 #pragma section? #pragma section? [examples of changing startup routine in connection with changing section name related to romization] here are examples of changing the startup routine (cstart.asm or cstartn.asm) and end module (rom.asm) in connection with changing a section name related to romization. (c source) #pragma section @@r_init rtt1 #pragma section @@init tt1 if a section name that stores an external variable with an initial value has been changed by describing #pragma section indicated above, the user must add to the startup routine the initial processing of the external variable to be stored in the new section. therefore, add the declaration of the first label of the new section and the portion that copies the initial value to the startup routine, and add the portion that declares the end label to the end module, as described below. rtt1_s and rtt1_e are the names of the first and end labels of section rtt1 , and tt1_s and tt1_e are the names of the first and end labels of section tt1 . (changing startup routine cstartx.asm) (1) add the declaration of the end label of the section whose name has been changed. extrn _main, _@stbeg, _hdwinit extrn rtt1_e, tt1_e add extrn declaration of rtt1_e, tt1_e
chapter 11 extended functions user?s manual u15556ej1v0um 385 #pragma section? #pragma section? (2) add the portion that copies the initial value from the rtt1 section whose name has been changed to the tt1 section. the initial value copying processing codes differ depending on the memory model. initial value copying processing can easily be added by copying the corresponding portion (initial value copying processing code) from the startup routine referring to the memory model specified by $_if , changing the symbols of the changed section _@r_init , _?r_init , etc. to rtt1_s , rtt1_e , etc., and adding the changed branch symbol (to ltt1 , etc.). : mov [de+],a br $ldats11 ldats12 : ; rtt1-> part added with tt1 copying processing (start) movg tde,#tt1_s movg whl,#rtt1_s ltt1 : subg whl,#rtt1_e be $ltt2 addg whl,#rtt1_e mov a,[hl+] mov [de+],a br $ltt1 ltt2 : ; rtt1 -> part added with tt1 copying processing (end) $_if(small) call !_main ;main(); $else call !!_main ;main(); $endif br $$ add portion that copies initial value from rtt1 section to tt1 section
chapter 11 extended functions user?s manual u15556ej1v0um 386 #pragma section? #pragma section? (3) set the first label of the section whose name has been changed. for the attribute of segment, refer to appendix b list of segment names . : $_if(small) @@rsins1 cseg base $else @@r_ins1 cseg $endif _@r_ins1: @@inis1 dseg saddr _@inis1: @@dats1 dseg saddr _@dats1: rtt1 cseg rtt1_s: add setting of label indicating beginning of section rtt1 tt1 dseg tt1_s: add setting of label indicating beginning of section tt1 $_if(small) base @@calfs cseg fixeda @@cnsts cseg base $endif $_if(medium) @@code cseg : ; end
chapter 11 extended functions user?s manual u15556ej1v0um 387 #pragma section? #pragma section? (changing end module rom.asm) (1) declare the label indicating the end of the section whose name has been changed. : $else name @rom $endif : public _?r_init,_?r_inis public _?init,_?data,_?inis,_?dats public _?r_ins1,_?inis1,_?dats1 public rtt1_e, tt1_e add rtt1_e and tt1_e ; $else @@init dseg _?init: @@data dseg _?data: $endif @@inis dseg saddr2 _?inis: @@dats dseg saddr2 _?dats: @@r_ins1 cseg _?r_ins1: @@inis1 dseg saddr _?inis1: @@dats1 dseg saddr _?dats1: $endif ;
chapter 11 extended functions user?s manual u15556ej1v0um 388 #pragma section? #pragma section? (2) set the label indicating the ends. : rtt1 cseg add setting of label indicating end of section rtt1 rtt1_e: tt1 dseg add setting of label indicating end of section tt1 tt1_e: ; end
chapter 11 extended functions user?s manual u15556ej1v0um 389 (20) binary constant binary constant binary constant 0bxxx function  describes binary constants at the location where integer constants can be described. effect  constants can be described in bit strings without being replaced with octal or hexadecimal numbers. readability is also improved. usage  describe binary constants in the c source. the following shows the description method for binary constants. 0b binary number 0b binary number remark binary number: either ?0? or ?1?  a binary constant has 0b or 0b at the start and is followed by the list of numbers 0 or 1.  the value of a binary constant is calculated with 2 as the base.  the type of a binary constant is the first one that can express the value in the following list. . non-subscripted binary number: int , unsigned int , long int unsigned long int . subscripted u or u: unsigned int , unsigned long int . subscripted l or l: long int unsigned long int . subscripted u or u and subscripted l or l: unsigned long int
chapter 11 extended functions user?s manual u15556ej1v0um 390 binary constant binary constant 0bxxx example (c source) unsigned i; i = 0b11100101; output object of compiler is the same as the following case. unsigned i; i = 0xe5; compatibility from another c compiler to this c compiler  modifications are not needed. from this c compiler to another c compiler  modification is required to meet the specifications of the compiler if the compiler supports binary constants.  modifications into other integer formats such as octal, decimal, and hexadecimal are needed if the compiler does not support binary constants.
chapter 11 extended functions user?s manual u15556ej1v0um 391 (21) module name changing function module name changing function #pragma name function  outputs the first eight letters of the specified module name to the symbol information table in an object module file.  outputs the first eight letters of the specified module name to the assemble list file as symbol information ( mod_nam ) when ?g2 is specified and as the name quasi directive when -ng is specified.  if a module name with nine or more letters is specified, a warning message is output.  if unauthorized letters are described, an error occurs and the processing is aborted.  if more than one of this #pragma directive exists, a warning message is output, and whichever is described later is enabled. effect  the module name of an object can be changed to any name. usage  the following shows the description method. #pragma name module name a module name must consist of the characters that the os authorizes as a file name except ?(? ?)?. upper case and lowercase letters are distinguished. example #pragma name module1 : compatibility from another c compiler to this c compiler  modification is not required if the compiler does not support the module name changing function.  when changing a module name, modify the program according to the method above. from this c compiler to another c compiler  delete #pragma name ? or delimit it with #ifdef .  when changing a module name, modify the program according to the specification of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 392 (22) rotate function rotate function #pragma rot function  outputs the code that rotates the value of an expression to the object with direct inline expansion instead of function call and generates an object file.  if there is not a #pragma directive, the rotate function is regarded as an ordinary function. effect  the rotate function can be realized using c source or asm description without describing the processing to perform rotate. usage  describe in the source in the same format as a function call. there are the following four function names. rorb, rolb, rorw, rolw [list of functions for rotate] (a) unsigned char rorb (x, y) ; unsigned char x ; unsigned char y ; rotates x to the right y times. (b) unsigned char rolb (x, y) ; unsigned char x ; unsigned char y ; rotates x to the left y times. (c) unsigned int rorw (x, y) ; unsigned int x ; unsigned char y ; rotates x to the right y times. (d) unsigned int rolw (x, y) unsigned int x ; unsigned char y ; rotates x to the left y times.
chapter 11 extended functions user?s manual u15556ej1v0um 393 rotate function #pragma rot  declare the use of the function for rotate by the #pragma rot directive of the module. however, the following items can be described before #pragma rot .  comments  other #pragma directives  preprocessing directives that neither define nor reference variables or functions.  keywords following #pragma can be described in either uppercase or lowercase letters. example (c source) #pragma rot unsigned char a = 0x11; unsigned char b = 2; unsigned char c; void main ( ) { c = rorb(a, b); } (output assembler source) with large model _main: mov c,!!_b mov a,!!_a ror a,1 dbnz c,$$-2 mov !!_c,a ret
chapter 11 extended functions user?s manual u15556ej1v0um 394 rotate function #pragma rot restrictions  the function names for rotate cannot be used as the function names.  the function names for rotate must be described in lowercase letters. if the functions for rotate are described in uppercase letters, they are handled as ordinary functions. compatibility from another c compiler to this c compiler  modification is not required if the compiler does not use the functions for rotate.  when changing to functions for rotate, modify the program according to the method above. from this c compiler to another c compiler  delete the #pragma rot statement or delimit it with #ifdef .  when using as a function for rotate, modification is required according to the specification of each compiler ( #asm , #endasm or asm() ; , etc.).
chapter 11 extended functions user?s manual u15556ej1v0um 395 (23) multiplication function multiplication function #pragma mul function  outputs the code that multiplies the value of an expression to an object with direct inline expansion instead of function call and generates an object file.  if there is not a #pragma directive, the multiplication function is regarded as an ordinary function. effect  codes utilizing the data size of the multiplication instruction i/o are generated. therefore, codes with faster execution speed and smaller size than the description of ordinary multiplication expressions can be generated. usage  describe in the same format as that of a function call in the source. there are the following three functions for multiplication. mulu, muluw, mulw [list of multiplication functions] (a) unsigned int mulu (x, y); unsigned char x; unsigned char y; performs unsigned multiplication of x and y. (b) unsigned long muluw (x, y); unsigned int x; unsigned int y; performs unsigned multiplication of x and y. (c) long mulw (x, y); int x; int y; performs signed multiplication of x and y.
chapter 11 extended functions user?s manual u15556ej1v0um 396 multiplication function #pragma mul  declare the use of functions for multiplication with the #pragma mul directive of the module. however, the following items can be described before #pragma mul .  comments  other #pragma directives  preprocessing directives that neither define nor reference variables or functions.  keywords following #pragma can be described in either uppercase or lowercase letters. restrictions  multiplication functions are handled as ordinary function if the target device does not have multiplication instructions.  the function names for multiplication cannot be used as the function names (when #pragma mul is declared).  the functions for multiplication must be described in lowercase letters. if they are described in uppercase letters, they are handled as ordinary function. example (c source) #pragma mul unsigned char a = 0x11; unsigned char b = 2; unsigned int i; void main() { i = mulu(a, b); } (output object of compiler) _main: mov a,!!_b mov b,!!_a mulu b movw !!_i,ax ret
chapter 11 extended functions user?s manual u15556ej1v0um 397 multiplication function #pragma mul compatibility from another c compiler to this c compiler  modification is not required if the compiler does not use the functions for multiplication.  when changing to functions for multiplication, modify the program according to the method above. from this c compiler to another c compiler  delete the #pragma mul statement or delimit it with #ifdef . function names for multiplication can be used as the function names.  when using as functions for multiplication, modification is required according to the specification of each compiler ( #asm , #endasm or asm() ;, etc.).
chapter 11 extended functions user?s manual u15556ej1v0um 398 (24) division function division function #pragma div function  outputs the code that divides the value of an expression to an object with direct inline expansion instead of function call and generates an object code file.  if there is not a #pragma directive, the function for division is regarded as an ordinary function. effect  codes utilizing the data size of the division instruction i/o are generated. therefore, codes with faster execution speed and smaller size than the description of ordinary division expressions can be generated. usage  describe in the same format as that of a function call in the source. there are the following two functions for division. divuw, moduw [list of division functions] (a) unsigned int divuw(x, y); unsigned int x; unsigned char y; performs unsigned division of x and y and returns the quotient. (b) unsigned char moduw(x, y); unsigned int x; unsigned char y; performs unsigned division of x and y and returns the remainder.  declare the use of the functions for division with the #pragma div directive of the module. however, the following items can be described before #pragma div .  comments  other #pragma directives  preprocessing directives that neither define nor reference variables or functions.  keywords following #pragma can be described in either uppercase or lowercase letters.
chapter 11 extended functions user?s manual u15556ej1v0um 399 division function #pragma div restrictions  the division function is handled as an ordinary function if the target device does not have division instructions.  the function names for division cannot be used as the function names.  the function names for division must be described in lowercase letters. if they are described in uppercase letters, they are handled as ordinary functions. example (c source) #pragma div unsigned int a = 0x1234; unsigned char b = 0x12; unsigned char c; unsigned int i; void main () { i = divuw(a, b); c = moduw(a, b); } (output object of compiler) with large model _main: mov b,!!_b movw ax,!!_a divuw b movw !!_i,ax mov b,!!_b movw ax,!!_a divuw b mov !!_c,b ret compatibility from another c compiler to this c compiler  modification is not required if the compiler does not use the functions for division.  when changing to functions for division, modify the program according to the method above. from this c compiler to another c compiler  delete the #pragma div statement or delimit it with #ifdef . the function names for division can be used as the function names.  when using as a function for division, modification is required according to the specification of each compiler ( #asm , #endasm or asm() ; , etc.).
chapter 11 extended functions user?s manual u15556ej1v0um 400 (25) data insertion function data insertion function #pragma opc function  inserts constant data into the current address.  when there is not a #pragma directive, the function for data insertion is regarded as an ordinary function. effect  specific data and instructions can be embedded in the code area without using the asm statement. when asm is used, an object cannot be obtained without going through the assembler. on the other hand, if the data insertion function is used, an object can be obtained without going through the assembler. usage  describe using uppercase letters in the source in the same format as that of a function call.  the function name for data insertion is _ _opc . [list of data insertion functions] (a) void _ _opc (unsigned char x, ); insert the value of the constant described in the argument to the current address. arguments can describe only constants.  declare the use of functions for data insertion with the #pragma opc directive. however, the following items can be described before #pragma opc .  comments  other #pragma directives  preprocessing directives that neither define nor reference variables or functions.  keywords following #pragma can be described in either uppercase or lowercase letters.
chapter 11 extended functions user?s manual u15556ej1v0um 401 data insertion function #pragma opc restrictions  the function names for data insertion cannot be used as the function names (when #opc is specified).  _ _opc must be described in uppercase letters. if it is described in lowercase letters, it is handled as an ordinary function. example (c source) #pragma opc void main ( ) { _ _opc(0xbf); _ _opc(0xa1, 0x12); _ _opc(0x10, 0x34, 0x12); } (output object of compiler) _main: ; line 4 : _ _opc (0xbf); db 0bfh ; line 5 : _ _opc (0xa1, 0x12); db 0a1h db 012h ; line 6 : _ _opc (0x10, 0x34, 0x12); db 010h db 034h db 012h ret compatibility from another c compiler to this c compiler  modification is not required if the compiler does not use the functions for data insertion.  when changing to functions for data insertion, modify the program according to the method above. from this c compiler to another c compiler  delete the #pragma opc statement or delimit it with #ifdef .  function names for data insertion can be used as function names. when using as a function for data insertion, modification is required according to the specification of each compiler ( #asm , #endasm or asm() ; , etc.).
chapter 11 extended functions user?s manual u15556ej1v0um 402 (26) interrupt handler for real-time os (rtos) interrupt handler for rtos #pragma rtos_interrupt ... function  interprets the function name specified by the #pragma rtos_interrupt directive as the interrupt handler for the 78k/iv series rtos (real-time os) rx78k/iv.  registers the address of the described function name to the interrupt vector table for the specified interrupt request name.  when a stack change is specified, the stack pointer is changed to the location where the offset is added to the array name symbol. the area of the array name is not secured by the #pragma directive. it needs to be defined separately as a global unsigned char type array. the two system call calling functions ret_int/ret_wup can be called in the interrupt handler for rtos (for the details of the system call calling function, refer to the list of rtos system call calling functions described later). if the prototype declaration or the entity definition of ret_int/ret_wup and ret_int/ret_wup are called outside the interrupt handler for rtos, an error occurs. the two rtos system call calling functions ret_int/ret_wup are called by an unconditional branch instruction. if there is neither ret_int nor ret_wup in the interrupt handler for rtos, an error occurs. if the interrupt request name and thereafter is omitted, only the two functions ret_int/ret_wup are enabled. the interrupt handler for rtos generates codes in the following order. (1) saves all the registers (2) changes the stack pointer (only when stack change is specified) (3) secures the local variable area (only when there is a local variable) (4) the function body (5) releases the local variable area (only when there is a local variable) (6) sets back the stack pointer (only when stack change is specified) (7) restores all the registers (8) reti for ret_int/ret_wup described in the middle of the function, the codes in (5) and (6) are generated immediately before the unconditional branch instruction each time. if a function ends with ret_int/ret_wup , the codes in (7) and (8) are not generated.
chapter 11 extended functions user?s manual u15556ej1v0um 403 interrupt handler for rtos #pragma rtos_interrupt ... effect  the interrupt handler for rtos can be described at the c source level.  because the interrupt request name is identified, the address of the vector table does not need to be identified. usage  the interrupt request name, function name, and stack change is specified by the #pragma directive. this #pragma directive is described at the start of the c source. when #pragma pc (type) is described, the main #pragma directive is described after #pragma pc . the following items can be described before #pragma directive.  comments  preprocessing directives that neither define nor reference variables or functions. #pragma ? rtos_interrupt [ ? interrupt request name ? function name ? [ stack change specification ]] remark stack change specification: sp = array name [+ offset location]  of the keywords to be described following #pragma , the interrupt request name must be described in uppercase letters. the other keywords can be described either in uppercase or lowercase letters. [list of rtos system call calling functions] (1) void ret_int ( ); calls rtos system call ret_int. (2) void ret_wup (x); char *x; calls rtos system call ret_wup with x as an argument.
chapter 11 extended functions user?s manual u15556ej1v0um 404 interrupt handler for rtos #pragma rtos_interrupt ... restrictions  interrupt request names are described in uppercase letters.  software interrupts and non-maskable interrupts cannot be specified for the interrupt request names. if specified so, an error occurs.  a duplication check on interrupt request names will be made within only one module.  if an interrupt (the same or another interrupt) is generated in duplicate during vector interrupt processing due to the contents of the priority specification flag register, interrupt mask flag register, etc., if the stack change is specified, the contents of the stack are updated, which may cause problems. however, this cannot checked by the compiler, so care must be taken.  callt/callf/noauto/norec/_ _callt/_ _callf/_ _leaf/_ _interrupt/_ _interrupt_brk/_ _pascal/_ _flash cannot be specified for the interrupt handler for rtos. the rtos system call calling function names ret_int/ret_wup cannot be used for the function names. if the functions that specified the stack change via the #pragma rtos_interrupt specification are not defined in the same module, a warning is output and the stack change specification is ignored. the interrupt handler for rtos is not supported when the static model is specified. example (a) when stack change is not specified (c source) #pragma rtos_interrupt intp0 intp int i; void intp ( ) { int a; a = 1; if (i == 1) { ret_int(); } }
chapter 11 extended functions user?s manual u15556ej1v0um 405 interrupt handler for rtos #pragma rtos_interrupt ... (output object of compiler) when -ml , -qv is specified (default) @@base cseg base _intp: push whl ; saves register push tde push uup push vvp push ax,bc,rp2,rp3 movw rp3,#01h ; allocates rp3 to variable a note movw ax,!!_i cmpw ax,rp3 bne $l0003 br !!_ret_int l0003; pop ax,bc,rp2,rp3 ; restores register pop vvp pop uup pop tde pop whl reti @@vect06 cseg at 0006h _@vect06: dw _intp note when the -qv option is not specified, the securing/releasing codes of the local variables are output after saving the register/before restoring the register, respectively.
chapter 11 extended functions user?s manual u15556ej1v0um 406 interrupt handler for rtos #pragma rtos_interrupt ... (b) when the stack change is specified (c source) #pragma rtos_interrupt intp0 intp sp=buff+10 int i; unsigned char buff[10]; extern unsigned short taskid1; void intp () { int a; a = 1; if (i == 1) { ret_wup (&taskid1); } } (output object of compiler) when -ml , -qv is specified (default) @@base cseg base _intp : push whl ; saves register push tde push uup push vvp push ax,bc,rp2,rp3 movg whl,sp movg sp,#_buff+10 ; changes stack pointer push whl movw rp3,#01h ; allocates rp3 to variable a note movw ax,!!_; cmpw ax,rp3 bne $l0003 movg uup,#_taskid1 note when the -qv option is not specified, the securing/releasing codes of the local variable area are output.
chapter 11 extended functions user?s manual u15556ej1v0um 407 interrupt handler for rtos #pragma rtos_interrupt ... (output object of compiler) when -ml , -qv is specified (default) pop whl ; sets back stack pointer movg sp,whl br !!_ret_wup l0003 : pop whl ; sets back stack pointer movg sp,whl pop ax,bc,rp2,rp3 ; restores register pop vvp pop uup pop tde pop whl reti @@vect06 cseg at 0006h _@vect06: dw _intp compatibility from another c compiler to this c compiler  modification is not required if the compiler does not support the interrupt handler for rtos.  when changing to the interrupt handler for rtos, modify the program according to the method above. from this c compiler to another c compiler  handled as an ordinary function if the #pragma rtos_interrupt specification is deleted.  when using as an interrupt handler for rtos, modification is required according to the specification of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 408 (27) interrupt handler qualifier for real-time os (rtos) interrupt handler qualifier for rtos __rtos_interrupt function  the function declared with the _ _rtos_interrupt qualifier is interpreted as an interrupt handler for rtos.  the two rtos system call calling functions ret_int/ret_wup can be called in the function declared with the keywords _ _rtos_interrupt (for details of the rtos system call calling functions, refer to list of rtos system call calling functions described later). if the prototype declaration or the entity definition of ret_int/ret_wup and ret_int/ret_wup are called outside the interrupt handler for rtos, an error occurs.  the functions to call the two rtos system call calling functions ret_int/ret_wup are called by an unconditional branch instruction.  if there is neither ret_int nor ret_wup in the interrupt handler for rtos, an error occurs. effect  the setting of the vector table and the definition of the interrupt handler function for rtos can be described in separate files. usage  _ _rtos_interrupt is added to the qualifier of the interrupt handler for rtos. _ _rtos_interrupt void func ( ) { processing } [list of the system call calling functions for rtos] (a) void ret_int ( ) ; calls system call ret_int for rtos. (b) void ret_wup (x) ; char *x ; calls system call ret_wup for rtos with x as an argument.
chapter 11 extended functions user?s manual u15556ej1v0um 409 interrupt handler qualifier for rtos __rtos_interrupt restrictions callt/callf/noauto/norec/_ _callt/_ _callf/_ _leaf/ _ _interrupt/_ _interrupt_brk/ _ _ pascal/_ _ flash cannot be specified for the interrupt handler for rtos.  the rtos system call calling function names ret_int/ret_wup cannot be used for the function names. cautions  vector addresses cannot be set only by declaring this qualifier. the setting of the vector address must be performed separately by the #pragma directive, assembler description, etc.  when the interrupt handler for rtos is defined in the same file as the one in which the #pragma rtos_interrupt is specified, the function name specified with #pragma rtos_interrupt is judged as an interrupt handler for rtos even if this qualifier is not described. compatibility from another c compiler to this c compiler  modification is not required if the compiler does not support interrupt handler for rtos.  when changing to interrupt handler for rtos, modify the program according to the method above. from this c compiler to another c compiler  changes can be made by #define (for details, refer to 11.6 modifications of c source ). by these changes, interrupt handler qualifiers for rtos are handled as ordinary variables.  when using as an interrupt handler for rtos, modification is required according to the specification of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 410 (28) task function for real-time os (rtos) task function for rtos #pragma rtos_task function  the function names specified with #pragma rtos_task are interpreted as the tasks for rtos.  if the function name is specified and the entity definition is not in the same file, an error occurs.  the preprocessing of the task function for rtos does not save the registers for frame pointer/register variables. the postprocessing is not output.  the following rtos system call calling functions can be used. [ rtos system call calling functions] (a) void ext_tsk (void); calls rtos system call ext_tsk. however, when ext_tsk is called in the ext_tsk prototype declaration or entity definition, interrupt function, or interrupt handler for rtos, an error occurs.  the rtos system call calling function of ext_tsk is called by an unconditional branch instruction. if ext_tsk is issued after the function, the postprocessing is not output.  when there is no ext_tsk in the task function for rtos and the -w2 option is specified, a warning message is output. effect  the task function for rtos can be described at the c source level.  the saving and postprocessing of the register frame pointer/register variable are not output, so the code efficiency is improved.
chapter 11 extended functions user?s manual u15556ej1v0um 411 task function for rtos #pragma rtos_task usage  specifies the function name for the following #pragma directives.  the #pragma directives are described at the start of the c source. however, the following items can be described before the #pragma directive.  comments  preprocessing directives that neither define nor reference variables or functions.  keywords following #pragma can be described either in uppercase or lowercase letters. #pragma ? rtos_task [ ? task-function-name ] restrictions  callt/callf/noauto/norec/_ _callt/_ _callf/_ _leaf/_ _interrupt/_ _interrupt brk/_ _rtos_interrupt/ _ _ pascal/_ _ flash cannot be specified for the task function for rtos.  the task function for rtos cannot be called in the same manner as ordinary functions. the rtos system call calling function name ext_tsk cannot be used for a function name. the task function for rtos is not supported when the medium model is specified. example (c source) #pragma rtos_task func void main ( ) { int a; a = 1; ext_tsk (); } void func ( ) { register int r; int x; x = 1; r = 2; ext_tsk (); }
chapter 11 extended functions user?s manual u15556ej1v0um 412 task function for rtos #pragma rtos_task (output object of compiler) when -ml , -qv is specified (default) @@code cseg _main : push rp3 movw rp3,#01h ; 1 br !!_ext_tsk ; epilogue is not output. _func : ; frame pointer is not saved. movw up,#01h ; 1 movw rp3,#02h ; 2 br !!_ext_tsk ; epilogue is not output. end compatibility from another c compiler to this c compiler  modification is not required if the compiler does not support the task function for rtos.  when changing to the task function for rtos, modify the program according to the method above. from this c compiler to another c compiler if the #pragma rtos_task specification is deleted, the rtos task function is used as an ordinary function. to use as rtos task function, modification is required according to the specification of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 413 (29) changing function call interface changing function call interface -zo function  arguments are passed in accordance with the former function interface specifications (in cc78k4 v1.00 compatible products, only the stack is used). for details of the function interface, refer to 11.7 function call interface . usage  the -zo option is specified during compilation. restriction  modules to which the -zo option is specified and modules to which the -zo option is not specified cannot be linked to one another.
chapter 11 extended functions user?s manual u15556ej1v0um 414 (30) changing the method of calculating the offset of arrays and pointers changing the method of calculating the offset of arrays and pointers -qh functions  when calculating the offset of arrays and pointers (distance from the start of the array or pointer), if the index is an int/short type variable, it is regarded as unsigned int/unsigned short , and if the index is a char type variable, it is regarded as unsigned char .  calculates the offset as a positive 64 kb or less.  however, the ordinary offset calculation is performed if the index is a long type variable or a constant. effect  the code efficiency is improved by performing unsigned offset calculation. usage  the -qh option is specified during compilation. restrictions  access to an object by array elements and pointers can be performed only when the offset is 64 kb or less.  the offset for the minus direction cannot be calculated. compatibility from another c compiler to this c compiler  when the index to arrays and pointers is a int/short type variable or char type variable and there is access to a minus-direction object or access to an object of more than 64 kb, the index is changed to a long type variable. otherwise, the -qh option should not be specified. from this c compiler to another c compiler  modification is not required.
chapter 11 extended functions user?s manual u15556ej1v0um 415 changing the method of calculating the offset of arrays and pointers -qh example (c source) int tabi [100]; char tabc [100]; int *iptr; void main (void) { long i = 50; int i = 30; char c = 2; tabi [i] = 1; /* unsigned offset calculation, 64 kb or less */ tabc [c] = 2; /* unsigned offset calculation, 64 kb or less */ tabi [l] = 3; /* signed offset calculation */ *(iptr + i) = 4; /* unsigned offset calculation, 64 kb or less */ *(iptr + (-i)) = 5; /* offset calculation, positive 64 kb or less */ *(iptr - i) = 6; /* signed offset calculation */ *(iptr -10) = 7; /* signed offset calculation */ *(iptr + (-10)) = 8; /* signed offset calculation */ } (output object of compiler) when -ml , -qh is specified (1/3) @@code cseg _main: push uup push rp3 push vvp ; line 6: long 1 = 50; movw rp3,#032h ;50 subw vp,vp ; line 7: int i = 30; movw up,#01eh ;30 ; line 8: char c= 2; mov c,#02h ;2 ; line 9: ; line 10 : tabi [i] = 1; /* unsigned offset calculation, 64 kb or less */ movw hl,up
chapter 11 extended functions user?s manual u15556ej1v0um 416 changing the method of calculating the offset of arrays and pointers -qh (output object of compiler) when -ml , -qh is specified (2/3) addw hl,hl ; offset calculation only for the lower 2 bytes movw ax,#01h ; 1 movw _tabi[hl],ax ; line 11 : tabc [c] = 2; /* unsigned offset calculation, 64 kb or less */ mov a,c xch a,b mov a,c mov _tabc[b],a ; offset calculation only for the least significant byte ; line 12 : tabi [l] = 3; /* signed offset calculation */ movw hl,rp3 mov a,r8 mov w,a addg whl,whl ; offset is 3 bytes, sign is considered addg whl,#_tabi movw ax,#03h ; 3 movw [h],ax ; line 13 : *(iptr + i) = 4; /* unsigned offset calculation, 64 kb or less */ movw hl,up movg tde,!!_iptr addw hl,hl ; offset calculation only for the lower 2 bytes addg tde,whl incw ax movw [de],ax ; line 14 : *(iptr + (-i)) = 5; /* offset calculation, positive 64 kb or less */ subw ax,ax subw ax,up movg whl,!!_iptr movw de,ax mov t,#00h ; 0 addw de,de ; offset calculation only for the lower 2 bytes addg whl,tde movw ax,#05h ; 5 movw [hl],ax
chapter 11 extended functions user?s manual u15556ej1v0um 417 changing the method of calculating the offset of arrays and pointers -qh (output object of compiler) when -ml , -qh is specified (3/3) ; line 15 : *(iptr - i) = 6 ; /* signed offset calculation */ movw hl,up mov a,h cvtbw mov w,a movg tde,!!_iptr addg whl,whl ; offset is 3 bytes subg tde,whl movw ax,#06h ; 6 movw [de],ax ; line 16 : *(iptr - 10) = 7 ; /* signed offset calculation */ movg whl,!!_iptr incw ax addg whl,#0ffffech ; -20 ; offset is a signed constant ( ? 20) movw [hl],ax ; line 17 ; *(iptr + (-10)) = 8 ; /* signed offset calculation */ movg whl,!!iptr incw ax addg whl,#0ffffech ; -20 ; offset is a signed constant ( ? 20) movw [hl],ax ; line 18 ; } pop vvp pop rp3 pop uup ret
chapter 11 extended functions user?s manual u15556ej1v0um 418 changing the method of calculating the offset of arrays and pointers -qh (output object of compiler) when -ml , -qh is not specified (1/3) @@code cseg _main : push uup push rp3 push vvp ; line 6: long i = 50; movw rp3,#032h ; 50 subw vp,vp ; line 7: int i = 30; movw up,#01eh ; 30 ; line 8: char c= 2; mov c,#02h ; 2 ; line 9: ; line 10 : tabi [i] = 1; /* unsigned offset calculation, 64 kb or less */ movw hl,up mov a,h cvtbw mov w,a addg whl,whl addg whl,#_tabi movw ax,#01h ; 1 movw [hl],ax ; line 11 : tabc [c] = 2; /* unsigned offset calculation, 64 kb or less */ mov a, c cvtbw movw hl,ax mov w,a addg whl,#_tabc mov a, c mov [hl],a ; line 12 : tabi [l] = 3; /* signed offset calculation */ movw hl,rp3 mov a,r8 mov w,a addg whl,whl addg whl,#_tabi movw ax,#03h ; 3 movw [hl],ax
chapter 11 extended functions user?s manual u15556ej1v0um 419 changing the method of calculating the offset of arrays and pointers -qh (output object of compiler) when -ml , -qh is not specified (2/3) ; line 13 : *(iptr + i) = 4; /* unsigned offset calculation, 64 kb or less */ movw hl,up movg tde,!!_iptr mov a,h cvtbw mov w,a addg whl,whl addg tde,whl movw ax,#04h ; 4 movw [de],ax ; line 14 : *(iptr + (-i)) = 5; /* offset calculation positive 64 kb or less */ subw ax,ax subw ax,up movg whl,!!_iptr movw de,ax cvtbw mov t,a addg tde,tde addg whl,tde movw ax,#05h ; 5 movw [hl],ax ; line 15 : *(iptr - i) = 6; /* signed offset calculation */ movw hl,up mov a,h cvtbw mov w,a movg tde,!!_iptr addg whl,whl subg tde,whl movw ax,#06h ; 6 movw [de],ax
chapter 11 extended functions user?s manual u15556ej1v0um 420 changing the method of calculating the offset of arrays and pointers -qh (output object of compiler) when -ml , -qh is not specified (3/3) ; line 16 : *(iptr - 10) = 7; /* signed offset calculation */ movg whl,!!_iptr incw ax addg whl,#0ffffech ; -20 movw [hl],ax ; line 17 : *(iptr + (-10)) = 8; /* signed offset calculation */ movg whl,!!_iptr incw ax addg whl,#0ffffech ; -20 movw [hl],ax ; line 18 : } pop vvp pop rp3 pop uup ret compatibility from another c compiler to this c compiler  when the index to arrays and pointers is a int/short type variable or char type variable and there is access to a minus-direction object or access to an object of more than 64 kb, the index is changed to a long type variable. otherwise, the -qh option should not be specified. from this c compiler to another c compiler  modification is not required.
chapter 11 extended functions user?s manual u15556ej1v0um 421 (31) pascal function pascal function _ _pascal function  generates the code that corrects the stack used for placing of arguments when a function is called on the called function side, not on the side calling the function. effect  object code can be shortened if a lot of function calls appear. usage  when a function is declared, a _ _pascal attribute is added to the beginning. restrictions  the pascal function does not support variable length arguments. if a variable length argument is defined, a warning is output and the _ _pascal keyword is disregarded.  in a pascal function, the keywords norec/_ _interrupt/_ _interrupt_brk/_ _rtos_interrupt/_ _flash cannot be specified. if they are specified, in the case of the norec keyword, the _ _pascal key word is disregarded and in the case of the _ _interrupt/_ _interrupt_brk/_ _rtos_interrupt/_ _flash keywords, an error is output.  the old specification function interface specification option ( -zo ) does not support the pascal function. when pascal functions are used, if -zo is specified, a warning message is output at the first place where a _ _pascal key word appears and the _ _pascal keywords in the input file are disregarded.  if a prototype declaration is incomplete, it won?t operate normally, so a warning message is output when a pascal function?s physical definition or prototype declaration is missing. explanation  the -zr option enables the change of all functions to the pascal function. however, if the pascal function is used to change functions that have few function calls, object code may increase. example (c source) _ _pascal int func(int a, int b, int c); void main() { int ret_val; ret_val = func(5, 10, 15);
chapter 11 extended functions user?s manual u15556ej1v0um 422 pascal function _ _pascal (c source) (continued) } _ _pascal int func(int a, int b, int c) { return (a + b + c); } (output object of compiler) with large model _main: push rp3 movw ax,#0fh ; push ax ; mov x,#0ah ; push ax ; mov x,#05h ; with the argument, a 4-byte stack is consumed. call $!_func ; here stack correction is not performed. movw rp3,bc pop rp3 ret _func: push rp3 movw rp3,ax movw ax,[sp+5] addw ax,rp3 movw bc,ax movw ax,[sp+7] addw bc,ax pop rp3 pop whl ; obtain the return address. pop ax,rp2 ; the 4-byte stack consumed on the calling side is corrected. br whl ; branch to the return address.
chapter 11 extended functions user?s manual u15556ej1v0um 423 pascal function _ _pascal compatibility from another c compiler to this c compiler ? modification is not required if the reserved word _ _ pascal is not used. ? when changing to the pascal function, modify the program according to the method above. from this c compiler to another c compiler ? compatibility is maintained by using #define . ? by this conversion, the pascal function is regarded as an ordinary function.
chapter 11 extended functions user?s manual u15556ej1v0um 424 (32) automatic pascal functionization of the function call interface automatic pascal functionization of the function call interface -zr function  with the exception of norec/_ _interrupt/_ _interrupt_brk/_ _rtos_interrupt/_ _flash and functions with variable length arguments, _ _pascal attributes are added to all functions. usage  the -zr option is specified during compilation. restrictions  the old specification function interface specification option ( -zo ) cannot be used at the same time. if this is used, a warning message is output and the -zr option is ignored.  modules in which the -zr option is specified and modules in which the -zr option is not specified cannot be linked. if a link is executed, it results in a link error. remark for details of the pascal function call interface, refer to 11.7.5 pascal function call interface .
chapter 11 extended functions user?s manual u15556ej1v0um 425 (33) flash area allocation method flash area allocation method -zf caution do not use this flash function for devices that have no flash area self-rewrite function. operation is not guaranteed if it is used. this function enables the flash memory rewrite function of devices. functions  generates an object file located in the flash area.  external variables in the flash area cannot be referenced from the boot area.  external variables in the boot area can be referenced from the flash area.  the same external variables and the same global functions cannot be defined in a boot area program and a flash area program. effect  enables locating a program in the flash area.  enables using function linking with a boot area object created without specifying the -zf option. usage  the -zf option is specified during compilation. restriction use startup routines or library for the flash area.
chapter 11 extended functions user?s manual u15556ej1v0um 426 (34) flash area branch table flash area branch table #pragma ext_table caution do not use this flash function for devices that have no flash area self-rewrite function. operation is not guaranteed if it is used. this function enables the flash memory rewrite function of devices. functions  determines the first address of the branch table for the startup routine, the interrupt function, or the function call from the boot area to the flash area.  the branch instruction, which is one of the branch table elements, occupies 4 bytes of area. 32 from the first address of the branch table are reserved as dedicated interrupt functions. ordinary functions are located after the ?first address of branch table +4 * 32.?  the branch table occupies 4* (32 + ext_func id max. value + 1) bytes of area. for the ext_func id value, refer to 11.5 (35) function call function from the boot area to the flash area . effect  a startup routine and interrupt function can be located in the flash area.  a function call can be performed from the boot area to the flash area. usage  the following #pragma directive specifies the first address of the flash area branch table. #pragma ? ext_table ? branch-table-first-address describe the #pragma directive at the beginning of the c source.  the following items can be described before the #pragma directive.  comments  #pragma directive other than #pragma ext_func, #pragma vect with ?zf specification, #pragma interrupt , or #pragma rtos_interrupt .  directives not to generate the definition/reference of variables or functions among the preprocessing directives.
chapter 11 extended functions user?s manual u15556ej1v0um 427 flash area branch table #pragma ext_table restrictions  the branch table is located at the first address of the flash area.  if #pragma ext_table does not exist before #pragma ext_func , #pragma vect with ?zf specification, #pragma interrupt , or #pragma rtos_interrupt , an error occurs.  the first address of the branch table is assumed to be 0x80 to 0xff80. however, match the first address value with the flash start address which is specified by the -zb linker option. if the address does not match, it results in a link error.  it is necessary to reconfigure the library for interrupt vectors ( _@vect100 to _@vect3e ) in accordance with the specified first address of the branch table. the default is 4000h in the interrupt vector library. to specify a value other than 0x4000, reconfigure the library as shown below. 1. change the place of h in itbltop equ 4000h of vect.inc in the \nectools32\src\cc78k4\src directory to the specified address. 2. run \nectools32\src\cc78k4\bat/repvect.bat in dos prompt, and update library using the assembler, etc. copy the updated library \nectools32\src\cc78k4\lib to \nectools32\lib78k4 to be used for linking. caution the above directory may differ depending on the installation method. compatibility from another c compiler to this c compiler  modification is not required if #pragma ext_table is not used.  when specifying the first address of the flash area branch table, change the address according to the method above. from this c compiler to another c compiler  delete the #pragma ext_table instruction or delimit it with #ifdef .  when specifying the first address of the flash area branch table, the following modification is required.
chapter 11 extended functions user?s manual u15556ej1v0um 428 flash area branch table #pragma ext_table example to generate the branch table after the address 4000h and allocate the interrupt function. (c source) #pragma ext_table 0x4000 #pragma interrupt intp0 intp void intp() { } (output object of compiler) (a) to allocate the interrupt function to the boot area (no -zf specification). public _@vect06 public _intp @@base cseg base _intp: reti @@vect06 cseg at 0006h _@vect06: dw _intp  set the first address of the interrupt function in the interrupt vector table.
chapter 11 extended functions user?s manual u15556ej1v0um 429 flash area branch table #pragma ext_table (b) to allocate the interrupt vector table to the flash area ( -zf specified). public _intp @ecode cseg _intp: reti @evect06 cseg at 0400ch br !!_intp (library for interrupt vector 06 ) public _@vect06 @@vect06 cseg at 0006h _vect06: dw 400ch  set the first address of the interrupt function in the branch table.  the first address of the branch table is 4000h and the interrupt vector address (2 bytes) is 0006h, so the address of the branch table becomes 4000h + 4*(0006h/2).  setting the 400ch address in the interrupt vector table is performed by the interrupt vector library.
chapter 11 extended functions user?s manual u15556ej1v0um 430 (35) function call function from the boot area to the flash area function call function from the boot area to the flash area #pragma ext_func caution do not use this flash function for devices that have no flash area self-rewrite function. operation is not guaranteed if it is used. this function enables the flash memory rewrite function of devices. functions  function calls from the boot area to the flash area are executed via the flash area branch table.  functions in the boot area can be called directly from the flash area. effect  it becomes possible to call a function in the flash area from the boot area. usage  the following #pragma directive specifies the function name and id value in the flash area called from the boot area. #pragma ? ext_func ? function-name ? id value this #pragma directive is described at the beginning of the c source. the following items can be described before this #pragma directive.  comments  directives that do not generate the definition/reference of variables or functions among the preprocessing directives. restrictions  the id value is set to 0 to 255 (0xff).  if #pragma ext_table does not exist before #pragma ext_func , it results in an error.  if the same function has a different id value or a different function has the same id value, an error occurs. (a) and (b) below are errors. (a) #pragma ext_func f1 3 #pragma ext_func f1 4 (b) #pragma ext_func f1 3 #pragma ext_func f2 3  if a function is called from the boot area to the flash area and there is no corresponding function definition in the flash area, the linker cannot conduct a check. this is the user?s responsibility.  the callt and callf functions can only be located in the boot area. if the callt and callf functions are defined in the flash area (when the -zf option is specified), it results in an error.
chapter 11 extended functions user?s manual u15556ej1v0um 431 function call function from the boot area to the flash area #pragma ext_func compatiblity from another c compiler to this c compiler  modification is not required if the #pragma ext_func is not used.  when performing the function call from the boot area to the flash area, modify the program according to the method above. from this c compiler to another c compiler  delete the #pragma ext_func instruction or delimit it with #ifdef .  when performing the function call from the boot area to the flash area, the following modification is required. example in the case that the branch table is generated after address 4000h and functions f1 and f2 in the flash area are called from the boot area. (c source) (1) boot area side #pragma ext_table 0x4000 #pragma ext_func f1 3 #pragma ext_func f2 4 extern void f1(void); extern void f2(void); void func() { f1(); f2(); }
chapter 11 extended functions user?s manual u15556ej1v0um 432 function call function from the boot area to the flash area #pragma ext_func (2) flash area side #pragma ext_table 0x4000 #pragma ext_func f1 3 #pragma ext_func f2 4 void f1() { } void f2() { }  #pragma ext_func f1 3 means that the branch destination to function f1 is located in branch table address 4000h + 4*32 + 4*3.  #pragma ext_func f2 4 means that the branch destination to function f2 is located in branch table address 4000h + 4*32 + 4*4.  4*32 bytes from the beginning of the branch table is exclusively for interrupt functions (including the startup routine). (output object of compiler) (1) boot area side (without -zf specification) @@code cseg _func: call !0408ch call !04090h ret (2) flash area side (with -zf specification) @ecode cseg _f1: ret _f2: ret @ext03 cseg at 0408ch br !!_f1 br !!_f2
chapter 11 extended functions user?s manual u15556ej1v0um 433 (36) firmware rom function firmware rom function _ _flash caution do not use this flash function for devices that have no flash area self-rewrite function. operation is not guaranteed if it is used. this function enables the flash memory rewrite function of devices. functions  this calls a firmware rom function that self-writes to the flash memory via the interface library positioned between the firmware rom function and the c language function.  in the interface library call interface, the first argument is passed via the register and the second and subsequent arguments are transferred to the stack. the first argument?s register is as follows. 1, 2-byte integer ax 3-byte integer whl 4-byte integer ax (lower integer), rp2 (higher integer)  the size of the pointer passed to the stack after the second argument is three bytes. effect  operations related to the firmware rom function can be described at the c source level. usage  _ _flash attributes are added to the top during an interface library prototype declaration. restrictions  function calls by a function pointer are not supported.  when the old specification function interface specification option ( -zo ) is specified, it results in an error.  when a function with _ _flash is defined, it results in an error. compatibility from another c compiler to this c compiler  modification is not required if the reserved word _ _flash is not used.  when changing the firmware rom function, modify the program according to the method above. from this c compiler to another c compiler  possible using #define (refer to 11.6 modifications of c source ).  in a cpu with a firmware rom function or substitute function, it is necessary for the user to create an exclusive library to access that area.
chapter 11 extended functions user?s manual u15556ej1v0um 434 (37) method of int expansion limitation of argument/return value method of int expansion limitation of argument/return value -zb function ? when the type definition of the function return value is char/unsigned char , the int expansion code of the return value is not generated. ? when the prototype of the function argument is defined and the argument definition of the prototype is char/unsigned char , the int expansion code of the argument is not generated. effect ? the object code is reduced and the execution speed improved since the int expansion codes are not generated. usage ? the -zb option is specified during compilation. example (c source) unsigned char func1 (unsigned char x, unsigned char y); unsigned char c, d, e; void main () { c = func1 (d, e); c = func2 (d, e); } unsigned char func1 (unsigned char x, unsigned char y) { return x + y; }
chapter 11 extended functions user?s manual u15556ej1v0um 435 method of int expansion limitation of argument/return value -zb (output object of compiler) when -zb is specified _main: ; line 5: c = func1 (d, e); mov x, !!_e ; do not execute int expansion push ax ; do not execute int expansion mov x, !!_d call $!_func1 pop ax mov !!_c,c ; line 6 c = func2 (d, e); mov x, !!_e mov a, #00h ; 0 ; execute int expansion since there is no prototype declaration push ax mov x, !!_d call !!_func2 pop ax mov !!_c,c ; line 7: } ret restrictions ? if the files are different between the definition of the function body and the prototype declaration to this function, the program may operate incorrectly. compatibility from another c compiler to this c compiler ? if the prototype declarations for all definitions of function bodies are not correctly performed, perform correct prototype declaration. alternatively, do not specify the -zb option. from this c compiler to another c compiler ? no modification is required.
chapter 11 extended functions user?s manual u15556ej1v0um 436 (38) memory manipulation function memory manipulation function #pragma inline function ? an object file is generated by the output of the standard library memory manipulation functions memcpy , memset, memchr, and memcmp with direct inline expansion instead of function call. ? when there is no #pragma directive, the code that calls the standard library functions is generated. effect ? compared with when a standard library function is called, the execution speed is improved. ? object code is reduced if a constant is specified for the specified character number. usage ? the function is described in the source in the same format as a function call. ? the following items can be described before #pragma inline . ? comments ? other #pragma directives ? preprocessing directives that do not generate variable definitions/references or function definitions/references example (c source) #pragma inline char ary1[100], ary2[100]; void main() { memset(ary1, ?a?, 50); memcpy(ary1, ary2, 50); }
chapter 11 extended functions user?s manual u15556ej1v0um 437 memory manipulation function #pragma inline (output object of compiler) when -ms is specified _main: ; line 7 : memset(ary1, 'a', 50); movw de,#_ary1 mov c,#032h ; 50 mov a,#041h ; 65 mov [de+],a dbnz c,$$-1 ; line 8 : memcpy(ary1, ary2, 50); movw de,#_ary1 mov c,#032h ; 50 movw hl,#_ary2 mov a,[hl+] mov [de+],a dbnz c,$$-2 ; line 9 : ; line 10 : p = memchr(ary1, 'b', 50); mov c,#032h ; 50 movw de,#_ary1 mov a,#042h ; 66 cmp a,[de] bz $l0006 incw de dbnz c,$$-5 subw de,de l0006: movw !_p,de ; line 11 : i = memcmp(ary1, ary2, 100); mov c,#064h ; 100 movw de,#_ary1 movw hl,#_ary2 mov a,[de+] sub a,[hl+] bnz $l0008 dbnz c,$$-5 l0008: subc x,x xch a,x movw !_i,ax ; line 12 : } ret
chapter 11 extended functions user?s manual u15556ej1v0um 438 memory manipulation function #pragma inline (output object of compiler) when -mm is specified _main: ; line 7 : memset(ary1, 'a', 50); movw de,#loww _ary1 mov c,#032h ; 50 mov a,#041h ; 65 mov [de+],a dbnz c,$$-1 ; line 8 : memcpy(ary1, ary2, 50); movw de,#loww _ary1 mov c,#032h ; 50 movw hl,#loww _ary2 mov w,#0fh ; 15 mov a,[hl+] mov [de+],a dbnz c,$$-2 ; line 9 : ; line 10 : p = memchr(ary1, 'b', 50); mov c,#032h ; 50 movw de,#loww _ary1 mov a,#042h ; 66 cmp a,[de] bz $l0006 incw de dbnz c,$$-5 subw de,de l0006: movw !!_p,de ; line 11 : i = memcmp(ary1, ary2, 100); mov c,#064h ; 100 movw de,#loww _ary1 movw hl,#loww _ary2 mov w,#0fh ; 15 mov a,[de+] sub a,[hl+] bnz $l0008 dbnz c,$$-5 l0008: subc x,x xch a,x movw !!_i,ax ; line 12 : } ret
chapter 11 extended functions user?s manual u15556ej1v0um 439 memory manipulation function #pragma inline (output object of compiler) when -ml is specified _main: ; line 7 : memset(ary1, 'a', 50); movg tde,#_ary1 mov c,#032h ; 50 mov a,#041h ; 65 mov [de+],a dbnz c,$$-1 ; line 8 : memcpy(ary1, ary2, 50); movg tde,#_ary1 mov c,#032h ; 50 movg whl,#_ary2 mov a,[hl+] mov [de+],a dbnz c,$$-2 ; line 9 : ; line 10 : p = memchr(ary1, 'b', 50); mov c,#032h ; 50 movg tde,#_ary1 mov a,#042h ; 66 cmp a,[de] bz $l0006 incg tde dbnz c,$$-6 subg tde,tde l0006: movg !!_p,tde ; line 11 : i = memcmp(ary1, ary2, 100); mov c,#064h ; 100 movg tde,#_ary1 movg whl,#_ary2 mov a,[de+] sub a,[hl+] bnz $l0008 dbnz c,$$-5 l0008: subc x,x xch a,x movw !!_i,ax ; line 12 : } ret
chapter 11 extended functions user?s manual u15556ej1v0um 440 memory manipulation function #pragma inline compatibility from another c compiler to this c compiler ? modification is not required if the memory manipulation function is not used. ? when changing the memory manipulation function, modify the program according to the method above. from this c compiler to another c compiler ? delete the #pragma inline directive or delimit it with #ifdef .
chapter 11 extended functions user?s manual u15556ej1v0um 441 (39) callf two-step branch function callf two-step branch function -zg function ? a function body to which the callf/_ _callf attribute is added is not allocated to the callf area from 800h to 0fffh, a branch instruction to the function body is allocated to the callf area, and the code to call the branch instruction using the callf instruction is generated. effect ? compared to the case when allocating a function body to the callf area, the callf/_ _callf attribute can be added to many more functions. therefore, this function can shorten the object code if many functions that include call functions are frequently used. usage ? the -zg option is specified during compilation. restrictions ? modules in which the -zg option is specified and modules in which the -zg option is not specified cannot be linked. ? the two-step branch table consumes 4 bytes per function when the -mm/ml option is specified, and 3 bytes when the -ms option is specified. the maximum number of callf functions that can be allocated when the -zg option is specified per load module and the total number of callf functions per linked module are as follows. - when the -mm/ml option is specified: 512 - when the -ms option is specified: 682 example (c source 1) _ _callf extern int fsub(); void main() { int ret_val; ret_val = fsub(); }
chapter 11 extended functions user?s manual u15556ej1v0um 442 callf two-step branch function -zg (c source 2) _ _callf int fsub() { int val = 1; return val; } (output object of compiler) with large or medium model (c source 1) extrn ?fsub ; declaration callf !?fsub ; call (c source 2) public _fsub ; declaration public ?fsub ; declaration @@calf cseg fixed ?fsub: br !!_fsub ; branch table @@code cseg _fsub: ; function definition . . function body . .
chapter 11 extended functions user?s manual u15556ej1v0um 443 callf two-step branch function -zg (output object of compiler) with small model (c source 1) extrn ?fsub ; declaration callf !?fsub ; call (c source 2) public _fsub ; declaration public ?fsub ; declaration @@calfs cseg fixeda ?fsub: br !_fsub ; branch table @@codes cseg base ; function definition _fsub: . . function body . .
chapter 11 extended functions user?s manual u15556ej1v0um 444 (40) automatic callf functionization of function call interface automatic callf functionization of function call interface -zh function ? the _ _callf attribute is added to all functions except for the callt/_ _callt / _ _interrupt/_ _interrupt_brk/_ _rtos_interrupt functions. usage ? the -zh option is specified during compilation. restrictions ? the -zf option for the flash area allocation specification cannot be specified at the same time. if specified, a warning message is output and the -zh option is ignored. ? the standard library that supports the -zf option is not available. sources that include the standard library cannot be linked using the -zf option during compilation.
chapter 11 extended functions user?s manual u15556ej1v0um 445 (41) three-byte address reference/generation function three-byte address reference/generation function #pragma addraccess function ? a code that references the highest byte and the lower 2 bytes of a 3-byte address, and a code that generates a 3-byte address from the value of the highest byte and the lower 2 bytes are output to an object directly with inline expansion and an object file is created. ? if the #pragma directive is not added, the three-byte address reference/generation function is regarded as an ordinary function. effect ? three-byte address reference/generation can be performed with a short code without using a complex cast description. usage ? describe the #pragma addraccess directive at the beginning of the c source. ? describe the #pragma addraccess directive in the c source in the same manner as a function call. ? the following items can be described before the #pragma addraccess directive. (1) comments (2) other #pragma directives (3) among the preprocessing directives, those that do not generate a variable definition/reference or function definition/reference. ? the keywords following #pragma addraccess can be described in either uppercase or lowercase letters. the following three names can be used for the three-byte address reference/generation function name. ? fp_seg ? fp_off ? mk_fp [list of function names for three-byte address reference/generation] (1) unsigned char fp_seg(void *addr); the value of the most significant byte of a three-byte address pointed by addr is obtained. (2) unsigned int fp_off(void *addr); the values of the lower 2 bytes of a three-byte address pointed by addr are obtained. (3) void *mk_fp(unsigned char seg, unsigned int offset); the address value of the three-byte address having the value pointed by seg as the most significant byte, and the value pointed by offset as the lower 2 bytes.
chapter 11 extended functions user?s manual u15556ej1v0um 446 three-byte address reference/generation function #pragma addraccess restrictions ? the function names for three-byte address reference/generation cannot be used as the function names. ? describe the three-byte address reference/generation function in uppercase letters. if lowercase letters are used, it is regarded as an ordinary function. ? when the small or medium model is specified, #pragma addraccess is ignored and the three-byte address reference/generation function is not supported. example #pragma addraccess unsigned char seg; unsigned int offset; unsigned char ary[10]; unsigned char *p; void main() { seg = fp_seg(ary); /* most significant byte value */ offset = fp_off(ary); /* value of lower 2 bytes */ p = mk_fp(seg, offset); /* generates 3-byte address */ } (output object of compiler) @@code cseg _main: ; line 8 : seg = fp_seg(ary); /* most significant byte value */ mov a,#highw _ary mov !!_seg,a ; line 9 : offset = fp_off(ary); /* value of lower 2 bytes */ movw ax,#loww _ary movw !!_offset,ax ; line 10 : ; line 11 : p = mk_fp(seg, offset); /* generates 3-byte address */ mov a,!!_seg mov w,a movw hl,!!_offset movg !!_p,whl ; line 12 : } ret
chapter 11 extended functions user?s manual u15556ej1v0um 447 three-byte address reference/generation function #pragma addraccess compatibility from another c compiler to this c compiler  modification is not required if the three-byte address reference/generation function is not used.  when specifying the three-byte address reference/generation function, modify the function according to the method above. from this c compiler to another c compiler  delete the #pragma addraccess statement or delimit it with #ifdef . the three-byte address reference/generation function name can be used as the function name.  when specifying the three-byte address reference/generation function, modify the function conforming to the specification of the c compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 448 (42) absolute address allocation specification absolute address allocation specification _ _directmap function ? the initial value of an external variable declared by _ _directmap and a static variable in a function is regarded as the allocation address specification, and variables are allocated to the specified addresses. ? the _ _directmap variable in the c source is treated as an ordinary variable. ? because the initial value is regarded as the allocation address specification, the initial value cannot be defined and remains an undefined value. ? the specifiable address specification range, secured area range linked by the module for securing the area for the specified addresses, and variable duplication check range are shown below. with small model address specification range secured area range duplication check range 0x80 to 0xffff 0xfd00 to 0xfeff 0xf000 to 0xfeff with large model ( -cs0 specified) address specification range secured area range duplication check range 0x80 to 0xffffff 0xfd00 to 0xfeff 0xf000 to 0xfeff with large model ( -cs15 specified) address specification range secured area range duplication check range 0x80 to 0xffffff 0xffd00 to 0xffeff 0xff000 to 0xffeff with medium model ( -cs0 specified) address specification range secured area range duplication check range 0xf000 to 0xffff 0xfd00 to 0xfeff 0xf000 to 0xfeff with medium model ( -cs15 specified) address specification range secured area range duplication check range 0xff000 to 0xfffff 0xffd00 to 0xffeff 0xff000 to 0xffeff ? if the address specification is outside the address specification range, an f799 error is output. ? if the allocation address of a variable declared by _ _directmap is duplicated and is within the duplication check range, a w762 warning message is output and the name of the duplicated variable is displayed. ? if the address specification range is inside the saddr1 area, the _ _sreg1 declaration is made automatically and the saddr1 instruction is generated. if the address specification range is inside the saddr2 area, the _ _sreg declaration is made automatically and the saddr2 instruction is generated. ? when the -csa option is specified, a w338 warning message is output and the _ _directmap declaration in the file is ignored. effect one or more variables can be allocated to the same arbitrary address.
chapter 11 extended functions user?s manual u15556ej1v0um 449 absolute address allocation specification _ _directmap usage ? declare _ _directmap in the module in which the variable to be allocated in an absolute address is to be defined. _ _directmap type name variable name = allocation address specification; _ _directmap static type name variable name = allocation address specification; _ _directmap _ _sreg type name variable name = allocation address specification; _ _directmap _ _sreg static type name variable name = allocation address specification; _ _directmap _ _sreg1 type name variable name = allocation address specification; _ _directmap _ _sreg1 static type name variable name = allocation address specification; ? if _ _directmap is declared for a structure/union/array, specify the address in braces {}. ? _ _directmap does not have to be declared in a module in which a _ _directmap external variable is referenced, so only declare extern . extern type name variable name; extern _ _sreg type name variable name; extern _ _sreg1 type name variable name; ? to generate the saddr2 instruction in a module in which a _ _directmap external variable allocated inside the saddr2 area is referenced, _ _sreg must be used together to make extern_ _sreg type name variable name;. ? to generate the saddr1 instruction in a module in which a _ _directmap external variable allocated inside the saddr1 area is referenced, _ _sreg1 must be used together to make extern_ _sreg1 type name variable name;. example (c source) _ _directmap char c = 0xff000; _ _directmap _ _sreg char d = 0xffd20; _ _directmap _ _sreg char e = 0xffd21; _ _directmap struct x char a; char b; char c; } xx = {0xffe30}; void main() { c = 1; d = 0x12; e.5 = 1; xx.a = 5; xx.c = 10; }
chapter 11 extended functions user?s manual u15556ej1v0um 450 absolute address allocation specification _ _directmap (output object) public _c public _d public _e public _xx public _main _c equ 0ff000h ; addresses for variables declared by _ _directmap _d equ 0ffd20h ; are defined by equ _e equ 0ffd21h ; _xx equ 0ffe30h ; extrn _ _mffd20 ; extrn output for linking secured area modules extrn _ _mffd21 ; extrn _ _mffe30 ; extrn _ _mffe31 ; extrn _ _mffe32 ; @@code cseg _main: ; line 11 : c = 1 ; mov !_c,#01h ; 1 ; line 12 : d = 0x12 ; mov _d,#012h ; saddr2 instruction output because address ; line 13 : e.5 = 1 ; specified in saddr2 area set1 _e.5 ; bit manipulation possible because _ _sreg also used ; line 14 : xx.a = 5 ; mov _xx,#05h ; saddr1 instruction output because address specified ; line 15 : xx.c = 10 ; in saddr1 area mov _xx+2,#0ah ; saddr1 instruction output because address specified ; in saddr1 area ; line 16 : } ret
chapter 11 extended functions user?s manual u15556ej1v0um 451 absolute address allocation specification _ _directmap restrictions ? _ _directmap cannot be specified for function arguments, return values, or automatic variables. if it is specified in these cases, an error occurs. ? if an address outside the secured area range is specified, the variable area will not be secured, making it necessary to either describe a directive file or create a separate module for securing the area. compatibility from another c compiler to this c compiler ? modification is not required if the keyword _ _directmap is not used. ? when changing to the _ _directmap variable, modify the program according to the method above. from this c compiler to another c compiler ? compatibility can be attained using #define (refer to 11.6 modifications of c source for details). ? when _ _directmap is being used as the absolute address allocation specification, modify the program according to the specifications of each compiler.
chapter 11 extended functions user?s manual u15556ej1v0um 452 11.6 modifications of c source by using the extended functions of this c compiler, efficient object generation can be realized. however, these extended functions are intended to cope with the 78k/iv series. so, to use them for other devices, the c source may need to be modified. here, how to make the c source portable from another c compiler to this c compiler and vice versa is explained. from another c compiler to this c compiler  #pragma note if the other c compiler supports the #pragma preprocessing directive, the c source must be modified. the method and extent of modifications to the c source depend on the specifications of the other c compiler.  extended specifications if the other c compiler has extended specifications such as addition of keywords, the c source must be modified. the method and extent of modifications to the c source depend on the specifications of the other c compiler. note #pragma is one of the preprocessing directives supported by ansi. the character string following #pragma is identified as a directive to the compiler. if the compiler does not support this directive, the #pragma directive is ignored and compilation will continue until it properly ends. from this c compiler to another c compiler because this c compiler has added keywords as the extended functions, the c source must be made portable to the other c compiler by deleting such keywords or delimiting them with #ifdef . example <1> to invalidate a keyword (same applies to callf , sreg , noauto , and norec etc.) #ifndef _ _k4_ _ #define callt /* makes callt an ordinary function */ #endif <2> to change from one type to another #ifndef _ _k4_ _ #define bit char /* changes bit type to char type variable */ #endif
chapter 11 extended functions user?s manual u15556ej1v0um 453 11.7 function call interface the following items will be explained concerning the interface between functions when a function is called. 1. return value (common in all the functions) 2. ordinary function call interface  passing arguments  location and order of storing arguments  location and order of storing automatic variables 3. noauto function call interface  passing arguments  location and order of storing arguments  location and order of storing automatic variables 4. norec function call interface  passing arguments  location and order of storing arguments  location and order of storing automatic variables 5. pascal function call interface
chapter 11 extended functions user?s manual u15556ej1v0um 454 11.7.1 return value the function called stores the return value in the registers and carry flags as shown in table 11-27. table 11-27. storage location of return values model type small model medium model large model 1-byte integer 2-byte integer bc bc bc 4-byte integer bc (lower) rp2 (higher) bc (lower) rp2 (higher) bc (lower) rp2 (higher) pointer bc bc (data pointer) whl (function pointer) tde structure, union bc (structure copied to the area specific to the function, the start address of the union) bc (structure copied to the area specific to the function, the start address of the union) tde (structure copied to the area specific to the function, the start address of the union) 1 bit cy (carry flag) cy (carry flag) cy (carry flag) floating-point number ( float type) bc (lower) rp2 (higher) bc (lower) rp2 (higher) bc (lower) rp2 (higher) floating-point number ( double type) bc (lower) rp2 (higher) bc (lower) rp2 (higher) bc (lower) rp2 (higher)
chapter 11 extended functions user?s manual u15556ej1v0um 455 11.7.2 ordinary function call interface when all the arguments are allocated to registers and there is no automatic variable, the ordinary function call interface is the same as noauto function call interface. (1) passing arguments (a) when the -zo option is not specified (default)  on the function call side, both the arguments declared with registers and the ordinary arguments are passed in the same manner. the second and subsequent arguments are passed via a stack, and the first argument is passed via a register or stack.  the location where the first argument is passed is shown in table 11-28 . table 11-28. location where first argument is passed (on function call side) option type when -zo is not specified when -zo is specified 1-byte integer note 2-byte integer ax passed via a stack 3-byte integer whl small model is passed via a stack passed via a stack 4-byte integer note ax, rp2 passed via a stack floating-point number ( float type) ax, rp2 passed via a stack floating-point number ( double type) ax, rp2 passed via a stack other passed via a stack passed via a stack note 1- to 4-byte data includes structures, unions, and pointers. (b) when the -zo option is specified  on the function call side, arguments declared with a register are passed via a register, and ordinary arguments are passed via a stack. for the registers used for passing, refer to table 11-30 .
chapter 11 extended functions user?s manual u15556ej1v0um 456 (2) location and order of storing arguments  there are two types of arguments: arguments allocated to registers and ordinary arguments. arguments allocated to registers are the arguments declared with registers and the arguments when -qv is specified.  the arguments not allocated to registers are allocated to stacks. the arguments allocated to stacks are placed on the stack sequentially from the last argument. (a) when the -zo option is not specified  saving and restoring registers to which arguments are allocated is performed on the function definition side.  when -qv option is specified, the ordinary arguments are also allocated to registers regarding they are declared with registers.  the ordinary arguments are allocated to a stack. when the arguments are passed via stacks, the area where the arguments are passed (stack) is used as the area to which arguments are allocated.  on the function definition side, the arguments that are passed via a register or stack are stored in the area to which arguments are allocated.  arguments with more references together with register variables are allocated to registers. when the -qf and -ml options are specified, however, a second or subsequent argument whose size is less than 4-bytes and number of references is two or less is not always allocated to a register. table 11-29. list of storing arguments (on function definition side, when -zo is not specified) model option small model, medium model note large model when -qf is specified rp3, vp, up rp3, vvp, uup when -qf is not specified rp3, vp rp3, vvp note with the medium model, the function pointer (3 bytes) cannot be used as a register argument. (order of allocation)  with small model, medium model, when -qf is specified char , int , short , enum type: if there is long , float , double type argument, in the order of up, rp3, vp char, int, short, enum type: if there is no long , float , double type argument, in the order of rp3, up, vp pointer type: in the order of up, vp, rp3 long , float , double type: rp3 (lower), vp (higher)  with small model, medium model, when -qf is not specified char , int , short , enum type: in the order of rp3, vp pointer type: in the order of vp, rp3 long , float , double type: rp3 (lower), vp (higher)  with large model, when -qf is specified char , int , short , enum type: if there is long , float , double type argument, in the order of up, rp3, vp char, int, short, enum type: if there is no long , float , double type argument, in the order of rp3, up, vp pointer type: in the order of uup, vvp long , float , double type: rp3 (lower), vp (higher)
chapter 11 extended functions user?s manual u15556ej1v0um 457  with large model, when -qf is not specified char , int , short , enum type: in the order of rp3, vp pointer type: in the order of vvp long , float , double type: rp3 (lower), vp (higher) (b) when the -zo option is specified  the locations where arguments are passed on the function call side and the function definition side are the location where arguments are allocated.  as long as there are allocable registers, the arguments declared with registers are allocated to registers.  the saving and restoring of registers to which arguments are allocated is performed before and after the function call. table 11-30. list of storing arguments (on function definition side, when -zo is specified) model option small model large model when -qf is specified rp3, vp, up rp3, vvp when -qf is not specified rp3, vp rp3, vvp (order of allocation)  with small model, when -qf is specified char, int, short, enum type: in the order of rp3, vp, up pointer type: in the order of vp, up , rp3 long, float, double type: rp3 (lower), vp (higher)  with small model, when -qf is not specified char, int, short, enum type: in the order of rp3, vp pointer type: in the order of vp, rp3 long, float, double type: rp3 (lower), vp (higher)  with large model char, int, short, enum type: in the order of rp3, vp pointer type: in the order of vvp long, float, double type: rp3 (lower), vp (higher)
chapter 11 extended functions user?s manual u15556ej1v0um 458 (3) location and order of storing automatic variables  there are two types of automatic variables: automatic variables to be allocated to registers and ordinary automatic variables. the automatic variables to be allocated to registers are the ones that are declared with registers and the automatic variables when -qv is specified. they are allocated to register _@kregxx as long as there are allocable registers and _@kregxx . however, the automatic variables are allocated to _@kregxx only when -qr is specified. the automatic variables allocated to registers and _@kregxx are called register variables hereafter. for _@kregxx , refer to appendix a list of labels for saddr area .  the register variables are allocated after register arguments are allocated. therefore, the register variables are allocated to registers when there are excess registers after the allocation of register arguments.  the automatic variables not allocated to registers are allocated to stacks.  the saving and restoring of registers and _@kregxx to allocate automatic variables is performed on the function definition side. (order of allocating automatic variables)  the order of allocating automatic variables to registers are the same as the order of allocating arguments. for the details, refer to the order of allocating arguments.  the automatic variables allocated to _@kregxx are allocated in the order of declaration.  the automatic variables allocated to stacks are placed on the stack in the order of declaration. the following shows an example of the interface above. example 1 (c source) void func0 (register int, int); void main () { func0 (0x1234, 0x5678); } void func0 (register int p1, int p2) { register int r; int a; r = p2; a = p1; }
chapter 11 extended functions user?s manual u15556ej1v0um 459 (output code) with large model, when -qf is specified and -zo is not specified @@code cseg _main: movw ax,#05678h ;22136 push ax ; arguments passed via stack movw ax,#01234h ;4660 ; the first argument is passed via register call $!_func0 ; function call pop ax ; arguments passed via stack ret _func0: push uup ; save registers for register variables/arguments push rp3 ; push vvp ; movw rp3,ax ; allocate register arguments to rp3 movw ax,[sp+11] ;p2 ; argument p2 to be passed via a stack movw up,ax ; register variable r (up) movw vp,rp3 ; register argument p1 (rp3) variable a (vp) pop vvp ; restores register for register variables/arguments pop rp3 pop uup ret
chapter 11 extended functions user?s manual u15556ej1v0um 460 11.7.3 noauto function call interface (1) passing arguments (a) when the -zo option is not specified (default)  on the function call side, the arguments declared with registers and the ordinary arguments are passed in the same manner. the second and subsequent arguments are passed via a stack. the first argument is passed via a register or a stack (in the same manner as ordinary functions).  for the location where the first argument is passed, refer to table 11-28 . (b) when the -zo option is specified  arguments are passed via registers. for the registers to be used, refer to table 11-13 . (2) location and order of storing arguments  on the function definition side, all the arguments are allocated to registers.  if there is an argument that cannot be allocated to a register, an error occurs. (a) when the -zo option is not specified (default)  on the function definition side, the arguments passed via registers or stacks are copied to registers. even when the arguments are passed via registers, the processing to copy the register is output because the register on the function call side (passing side) and the function definition side (receiving side) are different. for the registers allocated on the function definition side, refer to table 11-14 .  the saving and restoring of the register to which arguments are allocated is performed on the function definition side. (order of allocation)  the order is the same as an ordinary function with -qf specified. (b) when the -zo option is specified  the locations where arguments are passed on the function call side and the function definition side are the same as the locations where arguments are allocated.  the saving and restoring of registers to which arguments are allocated is performed before and after the function call. (order of allocation)  the order is the same as for ordinary functions.
chapter 11 extended functions user?s manual u15556ej1v0um 461 (3) location and order of storing automatic variables (a) when the -zo option is not specified (default) automatic variables are allocated to registers and _@kregxx . however, the automatic variables are allocated to _@kregxx only when -qr is specified. for _@kregxx , refer to appendix a list of labels for saddr area . automatic variables are allocated to registers when there are excess registers after the allocation of arguments. when -qr is specified, automatic variables are allocated also to _@kregxx . if an automatic variable cannot be allocated to registers and _@kregxx , an error occurs. the saving and restoring of the register and _@kregxx to which automatic variables is allocated are performed in the function definition side. (order of allocation)  the order of allocating automatic variables to registers are the same as the order of allocating arguments.  the automatic variables allocated to _@kregxx are allocated in the order of declaration. (b) when the -zo option is specified.  allocation cannot be performed because the automatic variables cannot be described. the following shows an example of the interface above. example (c source) noauto void func2 (int, int); void main ( ) { func2 (0x1234, 0x5678); } noauto void func2 (int p1, int p2) { /* function body */ }
chapter 11 extended functions user?s manual u15556ej1v0um 462 (output code) with small model, when -zo is specified @@codes cseg base _main: push rp3,vp ; save registers for arguments movw rp3,#01234h ;4660 ; allocate arguments to rp3 movw vp,#05678h ;22136 ; allocate arguments to vp call !_func2 ; function call pop rp3,vp ; restore registers for arguments ret _func2: ret (output code) with small model, when -zo is not specified @@codes cseg base _main: movw ax,#05678h ; 22136 push ax ; arguments passed via stack movw ax,#01234h ; 4660 ; the first argument is passed via register call !_func2 ; function call pop ax ; arguments passed via stack ret _func2: push rp3,up ; save registers for arguments movw rp3,ax ; allocate arguments to rp3 movw ax,[sp+7] ; argument passed via stack received by register movw up,ax ; allocate arguments to up pop rp3, up ; restore registers for arguments ret
chapter 11 extended functions user?s manual u15556ej1v0um 463 11.7.4 norec function call interface (1) passing arguments (a) when the -zo option is not specified (default) on the function call side, arguments are passed via registers and _@nrargx . for the registers, refer to table 11-17 registers used for norec function arguments: passing side ( without -zo ). (b) when the -zo option is specified on the function call side, arguments are passed via a register and _@nrargx . if the arguments cannot be passed via registers any more, they are passed only via _@nrargx instead of via registers. arguments are never passed via registers and _@nrargx together. (2) location and order of storing arguments  on the function definition side, all the arguments are allocated to registers and _@nrargx . however, arguments are allocated to _@nrargx only when -qr is specified. for _@nrargx , refer to appendix a list of labels for saddr area .  if there is an argument that cannot be allocated to registers and _@nrargx , an error occurs. (a) when the -zo option is not specified (default) ? on the function definition side, the arguments passed via registers are copied to registers. even when the arguments are passed via registers, copying the register is necessary because the register on the function call side (passing side) and the function definition side (receiving side) are different. when the arguments are passed via _@nrargx , the locations where arguments are passed are the same as the locations where arguments are allocated. if the arguments cannot be passed via registers any more, they are passed also via _@nrargx . arguments are passed via registers and _@nrargx together. the saving and restoring of the register to which arguments are allocated is performed in the function definition side. for the location of storing arguments, refer to table 11-18 registers used for norec function arguments: receiving side ( without -zo ).
chapter 11 extended functions user?s manual u15556ej1v0um 464 table 11-31. list of registers passing/receiving norec arguments (when -zo is not specified) model type small model, medium model note 1 large model note 2 the first argument is char type passed via c, de, rp2 received via r6, r7, vp, up passed via c, tde, rp2 received via r6, r7, vvp, up the first arguments is not char type passed via ax, de, rp2 received via rp3, vp, up passed via ax, tde, rp2 received via rp3, vvp, up notes 1. with the medium model, the function pointer (3 bytes) cannot be used via a register. when -qr is specified, however, it can be passed via _@nrargx . 2. with the large model, only one pointer (3 bytes) can be passed/received via a register. when -qr is specified, however, it can be passed/received also via _@nrargx . (order of allocation)  with small model, medium model char , int , short , enum type: if there is long , float , double type argument, in the order of up, rp3, vp if there is no long , float , double type argument, in the order of rp3, up, vp pointer type: in the order of up, vp, rp3 long , float , double type: rp3 (lower), vp (higher)  with large mode char , int , short , enum type: if there is long , float , double type argument, in the order of up, rp3, vp if there is no long , float , double type argument, in the order of rp3, up, vp pointer type: vvp long , float , double type: rp3 (lower), vp (higher) (b) when the -zo option is specified  the same as the noauto function call interface
chapter 11 extended functions user?s manual u15556ej1v0um 465 (3) location and order of storing automatic variables (a) when the -zo option is not specified the automatic variables are allocated to registers and _@nrargx as long as there are allocable registers and _@nrargx . if there is no allocable register any more, they are allocated to _@nratxx . however, automatic variables are allocated to _@nrargx and _@nratxx only when -qr is specified. for _@nratxx , refer to appendix a list of labels for saddr area if there is an automatic variable that cannot be allocated to registers, _@nrargx and _@nratxx , an error occurs. the saving and restoring of registers to which automatic variables are allocated is performed on the function definition side. (order of allocating automatic variables)  the order of allocating automatic variables to registers is the same as the order of allocating noauto function arguments. for details, refer to 11.7.3 noauto function call interface .  the automatic variables allocated to _@nratxx are allocated in the order of declaration. (b) when the -zo option is specified  the automatic variables are allocated to registers as long as there are allocable registers. if there are no more allocable registers, they are allocated to _@nratxx .  automatic variables are allocated to _@nratxx only when -qr is specified. for _@nratxx , refer to appendix a list of labels for saddr area .  the automatic variables are allocated after arguments are allocated. therefore, the automatic variables are allocated to registers when there are excess registers after the allocation of arguments.  if there is an automatic variable that cannot be allocated to a register and _@nratxx , an error occurs.  the saving and restoring of registers to allocate automatic variables is performed on the function definition side. (order of allocating automatic variables)  the order of allocating registers to automatic variables is the same as the order of allocating noauto function arguments. for details, refer to 11.7.3 noauto function call interface .  the automatic variables allocated to _@nrargx and _@nratxx are allocated in the order of declaration.
chapter 11 extended functions user?s manual u15556ej1v0um 466 example (c source) norec void func (int); void main (void) { func (0x34); } norec void func (int p1) { int a; a = p1; } (output code) with small model, when -qx2 and -zo are specified @@codes cseg _main: push rp3 ; save registers for arguments movw rp3,#034h; 52 ; allocate arguments to rp3 call $!_func3 ; function call pop rp3 ; restore registers for arguments ret _func: push vvp ; save the automatic variable register movw vp,rp3 ; a = p1 pop vvp ; restore the automatic variable register ret (output code) with small model, when -qx2 and -zo is not specified @@code cseg _main: movw ax,#034h ;52 ; transfers the argument at ax call $!_func ; function call ret _func: push uup ; save the automatic variable register push rp3 ; save registers for arguments movw rp3,ax ; store argument in rp3 movw up,rp3 ; a = p1 pop rp3 ; restore registers for arguments pop uup ; restore the automatic variable register ret
chapter 11 extended functions user?s manual u15556ej1v0um 467 11.7.5 pascal function call interface the difference between this function interface and other function interfaces is that the correction of stacks used for loading of arguments when a function is called is done by the function side that was called, rather than the function caller. all other points are the same as the function attributes specified at the same time. [area to which arguments are allocated] [sequence in which arguments are allocated] [area to which automatic variables are allocated] [sequence in which automatic variables are allocated] ? if the noauto attribute is specified at the same time, the features are the same as when a noauto function is called (refer to 11.7.3 noauto function call interface ). ? if the noauto attribute is not specified at the same time, the features are the same when an ordinary function is called (refer to 11.7.2 ordinary function call interface ). (c source) _ _pascal void func0 (register int, int); void main () { func0 (0x1234, 0x5678); } _ _pascal void func0 (register int p1, int p2) { register int r; int a; r = p2; a = p1; }
chapter 11 extended functions user?s manual u15556ej1v0um 468 (output code) with small model (when -qf option is specified) _main: ; line 4 : func0(0x1234, 0x5678); movw ax,#05678h ; 22136 push ax ; argument is passed via a stack movw ax,#01234h ; 4660 ; the first argument is passed via a register call !_func0 ; function call ; stack is not corrected here ret ; line 6 : _ _pascal void func0(register int p1, int p2) ; line 7 : { _func0: push rp3,up ; saves the register for register variables ; or register arguments movw rp3,ax ; allocates a register argument to rp3 push ax ; reserves the area for automatic variable a ; line 8 : register int r; ; line 9 : int a; ; line 10 : r = p2; movw ax,[sp+9]; p2 ; argument p2 is passed via stack movw up,ax ; register variable up ; line 11 : a = p1; movw ax,rp3 ; register argument rp3 movw [sp+0],ax ; a ; automatic variable a pop ax ; releases the area for automatic variable a pop rp3,up ; restores the register for register variables ; or register arguments pop hl ; obtains the return address incg sp ; pop ax ; the stack consumed by arguments passed via a ; stack is corrected br hl ; branch to the return address
chapter 11 extended functions user?s manual u15556ej1v0um 469 (c source) with large model _ _pascal noauto void func2(int, int); void main () { func2(0x1234, 0x5678); } _ _pascal noauto void func2(int p1, int p2) { ... } (output code) with large model _main: ; line 4 : func2(0x1234, 0x5678); movw ax,#05678h ; 22136 push ax ; argument is passed via a stack movw ax,#01234h ; 4660 ; the first argument is passed via a register call $!_func2 ; function call ; stack is not corrected here ret ; line 6 : _ _pascal noauto void func2(int p1, int p2) ; line 7 : { _func2: push uup ; saves the register for arguments push rp3 ; saves the register for arguments movw rp3,ax ; allocates a register argument to rp3 movw ax,[sp+8] ; argument passed via a stack and received by a register movw up,ax ; allocates an argument to up ... pop rp3 ; restores the register for arguments pop uup ; restores the register for arguments pop whl ; obtains the return address pop ax ; the stack consumed by arguments passed via a stack is corrected br whl ; branch to the return address
user?s manual u15556ej1v0um 470 chapter 12 referencing the assembler this chapter describes how to link a program written in assembly language. if a function called from a c source program is written in another language, both object modules are linked by the linker. this chapter describes the procedure for calling a program written in another language from a program written in the c language and the procedure for calling a program written in the c language from a program written in another language. how to interface with another language by using the ra78k4 assembler package and this c compiler is described in the following order. (1) calling assembly language routines from c language (2) calling c language functions from assembly language (3) referencing variables defined in c language (4) referencing variables defined in assembly language on the c language side (5) cautions
chapter 12 referencing the a ssembler user?s manual u15556ej1v0um 471 12.1 accessing arguments/automatic variables the procedure for accessing arguments and automatic variables of this c compiler is described below.  on the function call side, register arguments are passed in the same way as ordinary arguments. the first argument uses the following registers and stacks, and subsequent arguments are passed via stacks. table 12-1. passing arguments (function call side) type passing location (first argument) passing location (second and later arguments) 1-byte, 2-byte integer ax stack passing 3-byte integer whl (stack passing in case of small model) stack passing 4-byte integer ax, rp2 stack passing floating-point number ax, rp2 stack passing others stack passing stack passing remark 1- to 4-byte data includes structures and unions.  on the function definition side, arguments passed via a register or stack are stored in the argument allocation location. register arguments are copied to a register or saddr area ( _@kregxx ). even when passing is done via a register, the registers on the function call side (passing side) and the function definition side (receiving side) differ, and therefore register copying is performed. ordinary arguments passed via a register are placed on a stack on the function definition side. if passing is done via a stack, the passing location simply becomes the argument allocation location. saving and restoring of registers that allocate arguments is performed on the function definition side.  the arguments of functions and the values of automatic variables declared inside functions are stored in the following registers, saddr areas, or stack frames using an option. the base pointer used when storing in a stack frame uses the up register.
chapter 12 referencing the a ssembler user?s manual u15556ej1v0um 472 table 12-2. list of storing arguments/automatic variables (inside called function) option argument/auto variable storage location priority level -qv (register allocation option) declared argument or automatic variable ? with small or medium model rp3, vp, up (only when -qf is specified) ? with large model rp3, vvp, uup (only when -qf is specified) -qr register declared automatic variable ? with small or medium model rp3, vp, up (only when -qf is specified) ? with large model rp3, vvp, uup (only when -qf is specified) ? automatic variable _@kregxx -qrv declared argument or automatic variable ? with small or medium model rp3, vp, up (only when -qf is specified) ? with large model rp3, vvp, uup (only when -qf is specified) ? automatic variable _@kregxx although the allocation order may vary depending on the number of references, the priority level is determined basically by the following rules. <1> with small or medium model ? when -qf is specified char , int , short , enum type: in the order of up, rp3, vp (if a long , float , or double type argument exists) in the order of rp3, up, vp (if a long , float , or double type argument does not exist) pointer type: in the order of up, vp, rp3 long , float , or double type: rp3 (lower), vp (higher) ? when -qf is not specified char , int , short , enum type: in the order of rp3, vp pointer type: in the order of vp, rp3 long , float , or double type: rp3 (lower), vp (higher) <2> with large model ? when -qf is specified char , int , short , enum type: in the order of up, rp3, vp (if the long , float , or double type argument exists) in the order of rp3, up, vp (if the long , float , or double type argument does not exist) pointer type: in the order of uup, vvp, long , float , or double type: rp3 (lower), vp (higher) ? when -qf is not specified char , int , short , enum type: in the order of rp3, vp pointer type: in the order of vvp long , float , or double type: rp3 (lower), vp (higher) default declared argument, automatic variable stack frame order of appearance
chapter 12 referencing the a ssembler user?s manual u15556ej1v0um 473 the following example shows the function call. (c source: large model with -qrf ) void func0(register int, int); void main() { func0(0x1234, 0x5678); } void func0(register int p1, int p2) { register int r; int a; r = p2; a = p1; } (output assembler source) public _func0 public _main @@code cseg _main: movw ax,#05678h ; 22136 push ax ; argument is passed via a stack movw ax,#01234h ; 4660 ; the first argument is passed via a register call $!_func0 ; function call pop ax ; argument is passed via a stack ret _func0: push uup ; saves the register for arguments push rp3 movw rp3,ax ; allocates register arguments p1 to rp3 . push ax movw ax,[sp+10] ; p2 ; argument p2 passed via a stack is allocated to up movw up,ax movw ax,rp3 ; register argument p1 is assigned movw [sp+0],ax ; a ; to automatic variable a pop ax pop rp3 ; restores the register for arguments pop uup ret end
chapter 12 referencing the a ssembler user?s manual u15556ej1v0um 474 12.2 storing return values return values during function calls are stored in registers and carry flags. the storage locations of return values are shown in the table below. table 12-3. storage location of return values type small model medium model large model 1-byte integer 2-byte integer bc bc bc 4-byte integer bc (lower), rp2 (higher) bc (lower), rp2 (higher) bc (lower), rp2 (higher) pointer bc bc (data pointer) whl (function pointer) tde structure, union bc (start address of structure or union copied to function-specific area) bc (start address of structure or union copied to function- specific area) tde (start address of structure or union copied to function- specific area) 1 bit cy cy y floating-point number bc (lower), rp2 (higher) bc (lower), rp2 (higher) c (lower), rp2 (higher)

    
    
  !"#"$%&#  
   '()        !
"    #  !     '
*  #+$# ,-& &$&"$ ./) "+$#.0 $%  '()   !
" # &$ ' ("        '()        
 ")    '1 23, $ *  "+   !
!
 ",(     !$ - ,  ! "+   !
!
 
 ,
 - .# !  ! "  "+   !
!
 /"+ 
 / 0  !"&#"$%"+$#/#"&+0 / (" ,  !
!!"  "+   !
!
  + $ extern int func (int, long); 01
 10 void main ( ) { int i, j; long l; i = 1; l = 0x54321; j = func (i, l); 01
  10 } 2   !" ("      !"       (
  "         + (  + $ 3 4 '!
" "  
  4  
 $ '" !  !
" !

+  &$ 354 ,    4  
 +   
 $ '" 

   
 $ ' &  ""            4   
     +# ("  & & $ "&
' $+5&%$& return address to main l (lower word) l (higher word) low address stack pointer stack area arguments to be passed to func i high address

    
    6 /
0 7.&$#&$-%#&$##% !"&#"$/ &#"&+0 ' 4  
  "  (
  !$ 3 4 .# !+  #$ 354 .# !&! 364 , !  & .7+ 707 #$ % !"  !7 " 0"
""  !7 384 7 !+ 4  3 4 . !
 #
 34 9 !#! 3:4 9
  !    #$ . ("  
")  
  .7 
  &" 
 $'  ! 3 4 364  $%  '1    #  ! 3 4 364 $ / ("  "+   !
!!" + $ @@data dseg _dt1: ds (2) _dt2: ds (4) @@code cseg _func: push uup ;save work registers $$$$$$$$$ 354 push rp3 push vvp movw up,ax ;arg1 movw ax,[sp+11] ;arg2 movw rp3,ax movw ax,[sp+13] ;arg2 movw vp,ax movw !!_dt1,up ;move 1st argument(i) movw !!_dt2,rp3 ;move 2nd argument(l) movw !!_dt2+2,vp movw bc,#0ah ; 10 ;set return value $$$$$$$$$ 3 4 pop vvp ;restore work registers pop rp3 pop uup ret

    
     / + ;<(
  "+ ,
+$       & !#" "
  "+ ,
$ 3 4 .# !+  2 ("  .7
+
  '1   $ ' # !+  "$ 354 .# !&! 2 !"+,"  
  
# !! ! #+ $= #
#+ + !+
 +  !#
"
+#+ $ 2 !#+ 
+    &!  +#$ 364 , !  & .7+ 707 '#
& .7 + !+7.*7>7 
  
 $=     &   "
 + #   ! ;7  ! "   ;7 " 0"
" " 
+ !
" $ .7
 (" $'  !+   $ 384 7 !+ 4  > "  3 4
!364+# +   "  4  
 $ 3 4 . !
 #
 2 4      #
  
   
  #
      , !  975   , !?    ! !$=!
 #
    '
 #+$##%$#&$"&3" $ word bc register return value 16 bits or less higher word rp2 register return value 17 bits or more lower word bc register
chapter 12 referencing the a ssembler user?s manual u15556ej1v0um 478 <6> restoring the saved registers the saved contents of the base pointer and work registers are restored. <7> returning control to main figure 12-2. stack area after return higher word return address to main l (lower word) l (upper word) low address stack pointer stack area high address word return value bc register or rp2 register bc register lower word the procedure for calling an assembly language from c and the processing of the assembly language routine are illustrated in figure 12-3 . figure 12-3. calling assembly language routine from c return address to main l (lower word) l (higher word) low address stack pointer stack area arguments to be passed to func ax register high address higher word return address to main l (lower word) l (higher word) low address stack pointer stack area high address word return value bc register or rp2 register bc register lower word processing [func function] saving register (u)up, rp3, (v)vp* storing return value in bc or rp2, bc restoring registers [function main] i

    
    8
 "#"$%&# !"#"$ / 0 "%"+$#%&# !"/ &#"&+0 /
   ,  !
! + " "+   !
!
 +  !
$ 3 4 ,!
" !  "  !!
" 
  &$ 9   '
*#+$#,-& &$&"$ ./) "+$#.0 $ 354 , ,  !
!
 $ 364 , !#
& .7
"++!
"  (
"++!
" $ 384 9 
 #
,  !
!
  ,975 ,!$ / ("  "+   !
!!" + $ name func2 extrn _csub public _func2 code2 cseg _func2: movw ax, #20h ; .5 !
" @ push ax ; movw ax, #21h ; . !
"  call !_csub ; , a,. @b pop ax ret end

    
    *9 3 4 7  !!
"  & 2"!
"    
+c
 !
"   &$' !
"     
' $%  '()   ,
  #  !
"   &$ "&
'+&"$#%$+5 354 ,  !,  !
!
 '   
 "
+
 ,  !
!
 $ 364 , ! !#
& .7 '#
& .7"
+ !
"++!
"   &$2 ("  +
!
" 5++ 5#
 & $7>7 ("  384 9 !
 #
,975 , '
 #
 ,!975 ,!",  !
!
  $ 2nd argument stack area low address high address csub(i, j) 1st argument word bc register return value 16 bits or less higher word rp2 register return value 17 bits or more lower word bc register

    
    *
%&+3& :%. !)$-&" / 0 #;$#&%&$#'.%.7&  '(  #+ #+   ,  !
!!"  "+   !
!
  #+ "
+  <$& (   ,  !
!!"$ =<>#%>&#&? % !" ,
 extern void subf () ; char c = 0 ; int i = 0 ; void main () { subf () ; } 2 9/:de8"+  ,f #+ "
++ $ /"+ 
 public _subf extrn _c extrn _i @@code cseg _subf: mov a, #04h mov !!_c, a movw ax, #07h movw !!_i, ax ret end

    
    *
/
0 #;$#&%&$# &'.%.7& %&# '#+ #+   "+   !
!", #+ "
++  ,  !
!!" $ =<>#%">&#&? % !" ,
 extern char c ; extern int i ; void subf ( ) { c = ?a? ; i = 4 ; } 2 9/:de8"+  "+ f #+ "
++ $ name asmsub public _c public _i abc cseg _c: db 0 _i: dw 0 end

    
    *
6)$-& >#&$$$ / 0 @ab/".&+#&0 %,"  a g/.,22a =*b((  #+   "$ 2   !,!"("  a@h=i,  ?b  (   "a<=i,b$ extern int func(int, long); 01
 10 void main ( ) { int i, j; long l; i = 1; l = 0x54321; j = func (i, l); 01
  10 } 2 9/:de8 
  ""
++a<=i,b$ /
0 +$#%&"$#$-$+5 /!
"   & c
 " !
"   "!  $% '()   ,
 !
" #!$ "&
'+$#%&"$#$+5 return address to main l (lower word) l (higher word) low address j = func (i, i); stack area high address stack pointer ax register i
user?s manual u15556ej1v0um 484 chapter 13 effective utilization of compiler this chapter introduces how to effectively use this c compiler. 13.1 efficient coding when developing 78k/iv series microcontroller-applied products, efficient object generation may be realized with this c compiler by utilizing the saddr1/2 area, callt table, or callf area of the device. use of external variables if ( saddr2 area can be used) use sreg/_ _sreg variables/use compiler option ( -rd ). if ( saddr1 area can be used) use _ _sreg1 variables. use of bit type (one bit) data if ( saddr2 area can be used) use bit/boolean/_ _boolean type variables. if ( saddr1 area can be used) use _ _boolean1 type variables. definition of function if (the function is to be called frequently) if ( callt table can be used) declare it as _ _callt/callt function. (effective to shorten the code size) if ( callf area can be used) declare it as _ _callf/callf function. (effective to improve the execution speed)
chapter 13 effective utilization of compiler user?s manual u15556ej1v0um 485 (1) using external variables when defining an external variable, specify the external variable to be defined as a sreg/_ _sreg variable if the saddr2 area can be used. instructions to sreg/_ _sreg variables are shorter in code length than instructions to memory. this helps shorten object code and improve program execution speed. (the same can be also performed by specifying the -rd option, instead of using the sreg variable.) when saddr1 area as well as saddr2 area can be used, the similar effect can be achieved by specifying the external variable to be defined as _ _sreg1 variable. definition of sreg/_ _sreg variable: extern sreg int variable-name ; extern_ _sreg int variable-name ; remark refer to 11.5 (3) how to use the saddr area . (2) 1-bit data a data object which only uses 1-bit data should be declared as a bit type variable (or boolean/_ _boolean type variable). a bit manipulation instruction will be generated for an operation on a bit/boolean/_ _boolean type variable. because saddr area is used as well as the sreg variable, the codes can be shortened and the execution speed can be improved. when saddr1 area as well as saddr2 area can be used, the similar effect can be achieved by specifying the external variable to be defined as a _ _boolean1 type variable. declaration of bit/boolean type variable: bit variable-name ; boolean variable-name ; _ _boolean variable-name ; remark refer to 11.5 (7) bit type variables .
chapter 13 effective utilization of compiler user?s manual u15556ej1v0um 486 (3) function definitions for a function to be called over and over again, object code should be shortened or a structure which allows calling at high speeds should be provided. if the callt table can be used for functions to be called frequently, such functions should be defined as callt functions. likewise, if the callf area can be used for functions to be called frequently, such functions should be defined as callf functions. the callf functions can be called faster than ordinary function calls with shorter codes because the callf functions are called using the callf area of the device. the callt functions are effective when codes needs to be shortened because the callt functions use the callt area of the device and are called with shorter code than callf . definition of callt function: callt int tsub() { : } definition of callf function: callf int tsub() : } remark refer to 11.5 (1) callt function and 11.5 (15) callf function . in addition to the use of the areas shown above, objects that do not need modification of the c source by compiling with the optimization option can be generated. for the effect of each -q suboption, refer to the cc78k4 c compiler operation user?s manual (u15557e) . (4) optimization option the optimization options that emphasize the object code size the most is as follows. [object code is emphasized the most] -qx3 further shortening of the code size and improvement of the execution speed is possible by adding _ _sreg or _ _sreg1 to variables. however, this is restricted to the cases when saddr2 area or saddr1 area can be used. when the areas have no more space and cannot be used, a compilation error occurs. if execution speed is also highly emphasized, specify the -qx2 default. if the code size is smaller than -qx3 , -qx4 can be specified. however, there are restrictions during debugging.
chapter 13 effective utilization of compiler user?s manual u15556ej1v0um 487 in addition, the object efficiency can be improved by adding the extended functions supported by this compiler to the c source. (5) using extended functions  definition of function if (the function is to be called frequently) if (the function is not to be used recursively) declare it as _ _leaf/norec functions. if (the function does not use automatic variables) declare it as noauto function. if (the function uses automatic variables and && register/saddr area can be used) declare it with register storage class. if (use internal static variables) && ( saddr2 area can be used) declare with _ _sreg /specify -rs option  functions not used recursively of the functions to be called over and over again, the ones which are not used recursively should be defined as _ _leaf/norec functions. norec functions become functions that do not have preprocessing/ postprocessing (stack frame). therefore, the object code can be shortened and the execution speed can be improved compared to the ordinary functions. remark for the definition of the norec function ( norec int rout ()... ), refer to 11.5 (6) norec function and 11.7.4 norec function call interface .
chapter 13 effective utilization of compiler user?s manual u15556ej1v0um 488  functions that do not use automatic variables functions that do not use automatic variables should be defined as noauto functions. these functions will not output code for stack frame generation and their arguments will be passed to registers as much as possible. these functions help shorten object code and improve program execution speed. remark for the definition of the noauto function ( noauto int sub1 (int i)... ), refer to 11.5 (5) noauto functions and 11.7.3 noauto function call interface .  functions that use automatic variables if the saddr2 area can be used for a function that uses automatic variables, declare the function with the register storage class specifier. by this register declaration, the object declared as register will be allocated to a register. a program using registers operates faster than one using memory, and object code can be shortened as well. remark for the definition of the register variable ( register int i; ... ), refer to 11.5 (2) register variables .  functions that use internal static variables if the saddr2 area can be used for a function that uses internal static variables, declare the function with _ _sreg or specify the -rs option. in the same way as with sreg variables, the object code can be shortened and the execution speed can be improved. when saddr1 area can be used as well as saddr2 area, the same effect can be achieved by declaring the function with _ _sreg1 . remark refer to 11.5 (3) how to use saddr area .
chapter 13 effective utilization of compiler user?s manual u15556ej1v0um 489 in addition, the code efficiency and the execution speed can be improved by the following methods.  use of sfr name (or sfr bit name). #pragma sfr use of _ _sreg/_ _sreg1 declaration for bit fields that consist only of 1-bit members (unsigned char type can be used for members). _ _sreg struct bf { unsigned char a:1; unsigned char b:1; unsigned char c:1; unsigned char d:1; unsigned char e:1; unsigned char f:1; } bf_1;  use of the register bank change for interrupt processing. #pragma interrupt intp0 inter rb1  use of multiplication and division embedded function. #pragma mul #pragma div  description of only the modules whose speed needs to be improved in the assembly language.
user?s manual u15556ej1v0um 490 appendix a list of labels for saddr area with the cc78k4, addresses in the saddr2 area are referenced by the following label names. for this reason, the same names as these label names cannot be used in the c source program or assembler source program. for the areas of section a.1 to a.3, any consecutive 32-byte area of saddr2 area (f) fd20h to (f) fdffh is used. the allocation addresses are determined at linking. remark (f)fdxxh indicates the address where _@nrarg0 is allocated, and f is added to the higher 4 bits at the location 1024k (0fh: compiler option -cs15 ). a.1 arguments of norec functions label name address _@nrarg0 (f)fdxxh _@nrarg1 _@nrarg0 + 1h _@nrarg2 _@nrarg0 + 2h _@nrarg3 _@nrarg0 + 3h _@nrarg4 _@nrarg0 + 4h _@nrarg5 _@nrarg0 + 5h _@nrarg6 _@nrarg0 + 6h _@nrarg7 _@nrarg0 + 7h
appendix a list of labels for saddr area user?s manual u15556ej1v0um 491 a.2 automatic variables of norec functions label name address _@nrat00 _@nrarg0 + 8h _@nrat01 _@nrarg0 + 9h _@nrat02 _@nrarg0 + ah _@nrat03 _@nrarg0 + bh _@nrat04 _@nrarg0 + ch _@nrat05 _@nrarg0 + dh _@nrat06 _@nrarg0 + eh _@nrat07 _@nrarg0 + fh a.3 register variables label name address _@kreg00 _@nrarg0 + 10h _@kreg01 _@nrarg0 + 11h _@kreg02 _@nrarg0 + 12h _@kreg03 _@nrarg0 + 13h _@kreg04 _@nrarg0 + 14h _@kreg05 _@nrarg0 + 15h _@kreg06 _@nrarg0 + 16h _@kreg07 _@nrarg0 + 17h _@kreg08 _@nrarg0 + 18h _@kreg09 _@nrarg0 + 19h _@kreg10 _@nrarg0 + 1ah _@kreg11 _@nrarg0 + 1bh _@kreg12 _@nrarg0 + 1ch _@kreg13 _@nrarg0 + 1dh _@kreg14 _@nrarg0 + 1eh _@kreg15 _@nrarg0 + 1fh
user?s manual u15556ej1v0um 492 appendix b list of segment names this chapter explains all the segments that the compiler outputs and their locations. (1) to (3) shows the options and re-allocation attributes used in the table. (1) option ? ? ? ? ms : small model ? ? ? ? mm : medium model ? ? ? ? ml : large model ? ? ? ? cs0 : location 00h ? ? ? ? cs15 : location 0fh (2) relocation attribute of cseg callt0 : allocates the specified segment in the address 40h to 7fh with the start address of a multiple of 2. base : allocates the specified segment in the address 80h to 0fcffh. at absolute expression: allocates the specified segment in an absolute address (within 0h to 0fcffh, 10000h to 0fffffh) note . fixed : allocates the start address of the specified segment in the address 800h to 0fffh. fixeda : allocates the start address of the specified segment in the address 800h to 0fffh and the end within 0fcffh. page : allocates the specified segment in the address xxx00h to xxxffh (within 0fffffh). page64k : allocates the specified segment not to extend over the 64 kb boundary (within 0h to 0fcffh, 10000h to 0fffffh) note . unit /without specification: allocates the specified segment to a given location (within 80h to 0fcffh, 10000h to 0fffffh) note . unitp : allocates the specified segment to a given location with the start address in an even address (80h to 0fcffh, 10000h to 0fffffh) note . note the range can be changed by specifying the ? ? ? ? cs option.
appendix b list of segment names user?s manual u15556ej1v0um 493 (3) re-allocation attributes of dseg saddr : allocates the specified segment to saddr1 area (saddr1 area: 0fe00h to 0feffh) note saddr2 : allocates the specified segment to saddr2 area (saddr2 area: 0fd20h to 0fdffh) note saddrp : allocates the specified segment starting from an even address in saddr1 area. saddrp2 : allocates the specified segment starting from an even address in saddr2 area. saddra : allocates the specified segment to a given area in saddr area ( saddr area: saddr1 area/ saddr2 area). at absolute expression: allocates the specified segment to an absolute address. unit /without specification: allocates the specified segment to a given location (within the memory area name ?ram?) note . unitp : allocates the specified segment to a given location starting from an even address (within the memory area name ?ram?) note . page : allocates the specified segment to a given location between xxxx00h to xxxxffh (within 0fffffh) note . page64k : allocates the specified segment not to extend over the 64 kb boundary (within 0h to 0fcffh, 10000h to fffffh) note . note the range can be changed by specifying the ? ? ? ? cs option (the address may differ depending on the target device. for details, refer to the user's manual of the target device used).
appendix b list of segment names user?s manual u15556ej1v0um 494 b.1 list of segment names b.1.1 program area and data area (1) with small model (when ? ? ? ? ms is specified) section name segment type relocation attribute description @@base cseg base segment for callt function and interrupt function @@vectnn cseg at nnh segment for interrupt vector table @@codes cseg base segment for ordinary function codes @@cnsts cseg base segment for const variables @@calfs cseg fixeda segment for callf function @@calt cseg callt0 segment for table for callt function @@rsinit cseg base segment for initialization data (with initial value) @@rsinis cseg base segment for initialization data ( sreg variable with initial value) @@rsins1 cseg base segment for initialization data ( sreg1 variable with initial value) @@init dseg segment for data area (with initial value) @@data dseg segment for data area (without initial value) @@inis dseg saddr2 segment for data area ( sreg variable with initial value) @@dats dseg saddr2 segment for data area ( sreg variable without initial value) @@inis1 dseg saddr segment for data area ( sreg1 variable with initial value) @@dats1 dseg saddr segment for data area ( sreg1 variable without initial value) @@bits bseg sa ddr2 segment for boolean type and bit type variables @@bits1 bseg saddr segment for _ _boolean 1 type variable @ext00 cseg at04080h segment for the flash area branch table (only when -zf is specified) note note when -zf is specified, the second ?@? from the top is changed to ?e? in the section name. for details, refer to b.1.2 flash memory area ( @@inis @einis , etc.). also, it is possible to change the address of the relocation attribute using #pragma ext_table . remark for @@vectnn , nn is determined when the interrupt source is specified by #pragma vect ( interrupt ) ( nn : number of interrupt vector address).
appendix b list of segment names user?s manual u15556ej1v0um 495 (2) with large model (when ? ? ? ? ml is specified) section name segment type relocation attribute description @@base cseg base segment for callt function and interrupt function @@vectnn cseg at nnh segment for interrupt vector table @@code cseg segment for ordinary function codes @@cnst cseg segment for const variables @@calf cseg fixed segment for callf function @@calt cseg callt0 segment for table for callt function @@r_init cseg segment for initialization data (with initial value) @@r_inis cseg segment for initialization data ( sreg variable with initial value) @@r_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @@init dseg segment for data area (with initial value) @@data dseg segment for data area (without initial value) @@inis dseg saddr2 segment for data area ( sreg variable with initial value) @@dats dseg saddr2 segment for data area ( sreg variable without initial value) @@inis1 dseg saddr segment for data area ( sreg1 with initial value) @@dats1 dseg saddr segment for data area ( sreg1 variable without initial value) @@bits bseg sa ddr2 segment for boolean type and bit type variables @@bits1 bseg saddr segment for _ _boolean1 type variable @ext00 cseg at04080h segment for the flash area branch table (only when -zf is specified) note note when -zf is specified, the second ?@? from the top is changed to ?e? in the section name. for details, refer to b.1.2 flash memory area (@@inis @einis , etc.). also, it is possible to change the address of the relocation attribute using #pragma ext_table . remark for the @@vectnn , nn is determined when the interrupt source is specified by #pragma vect ( interrupt ) ( nn : number of interrupt vector address).
appendix b list of segment names user?s manual u15556ej1v0um 496 (3) with medium model and location 00h (when ? ? ? ? mm and ? ? ? ? cs0 are specified) section name segment type relocation attribute description @@base cseg base segment for callt function and interrupt function @@vectnn cseg at nnh segment for interrupt vector table @@code cseg segment for ordinary function codes @@cnsts cseg base segment for const variables @@calf cseg fixed segment for callf function @@calt cseg callt0 segment for table for callt function @@r_init cseg segment for initialization data (with initial value) @@r_inis cseg segment for initialization data ( sreg variable with initial value) @@r_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @@init dseg segment for data area (with initial value) @@data dseg segment for data area (without initial value) @@inis dseg saddr2 segment for data area ( sreg variable with initial value) @@dats dseg saddr2 segment for data area ( sreg variable without initial value) @@inis1 dseg saddr segment for data area ( sreg1 variable with initial value) @@dats1 dseg saddr segment for data area ( sreg1 variable without initial value) @@bits bseg sa ddr2 segment for boolean type and bit type variables @@bits1 bseg saddr segment for _ _boolean1 type variable @ext00 cseg at04080h segment for the flash area branch table (only when -zf is specified) note note when -zf is specified, the second ?@? from the top is changed to ?e? in the section name. for details, refer to b.1.2 flash memory area ( @@inis @einis , etc.). also, it is possible to change the address of the relocation attribute using #pragma ext_table . remark for the @@vectnn , nn is determined when the interrupt source is specified by #pragma vect ( interrupt ) ( nn : number of interrupt vector address).
appendix b list of segment names user?s manual u15556ej1v0um 497 (4) with medium model and location 0fh (when ? ? ? ? mm and ? ? ? ? cs15 are specified) section name segment type relocation attribute description @@base cseg base segment for callt function and interrupt function @@vectnn cseg at nnh segment for interrupt vector table @@code cseg segment for ordinary function codes @@cnstm cseg page64k segment for const variables @@calf cseg fixed segment for callf function @@calt cseg callt0 segment for table for callt function @@r_init cseg segment for initialization data (with initial value) @@r_inis cseg segment for initialization data ( sreg variable with initial value) @@r_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @@initm dseg page64k segment for data area (with initial value) @@datam dseg page64k segment for data area (without initial value) @@inis dseg saddr2 segment for data area ( sreg variable with initial value) @@dats dseg saddr2 segment for data area ( sreg variable without initial value) @@inis1 dseg saddr segment for data area ( sreg1 variable with initial value) @@dats1 dseg saddr segment for data area ( sreg1 variable without initial value) @@bits bseg sa ddr2 segment for boolean type and bit type variables @@bits1 bseg saddr segment for _ _boolean1 type variable @ext00 cseg at04080h segment for the flash area branch table (only when -zf is specified) note note when -zf is specified, the second ?@? from the top is changed to ?e? in the section name. for details, refer to b.1.2 flash memory area ( @@inis @einis , etc.). also, it is possible to change the address of the relocation attribute using #pragma ext_table . remark for the @@vectnn , nn is determined when the interrupt source is specified by #pragma vect ( interrupt ) ( nn : number of interrupt vector address).
appendix b list of segment names user?s manual u15556ej1v0um 498 b.1.2 flash memory area (1) with small model (when ? ? ? ? ms is specified) section name segment type relocation attribute description @ecodes cseg base segment for normal function codes @ecnsts cseg base segment for const variables @ersinit cseg base segment for initialization data (with initial value) @ersinis cseg base segment for initialization data ( sreg variable with initial value) @ersins1 cseg base segment for initialization data ( sreg1 variable with initial value) @einit dseg segment for data area (with initial value) @edata dseg segment for data area (without initial value) @einis dseg saddr2 segment for data area ( sreg variable with initial value) @edats dseg saddr2 segment for data area ( sreg variable without initial value) @einis1 dseg saddr segment for data area ( sreg1 variable with initial value) @edats1 dseg saddr segment for data area ( sreg1 variable without initial value) @ebits bseg sa ddr2 segment for boolean type and bit type variables @ebits1 bseg saddr segment for _ _boolean 1 type variable (2) with large model (when ? ? ? ? ml is specified without 2-byte alignment) section name segment type relocation attribute description @ecode cseg segment for normal function codes @ecnst cseg segment for const variables @er_init cseg segment for initialization data (with initial value) @er_inis cseg segment for initialization data ( sreg variable with initial value) @er_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @einit dseg segment for data area (with initial value) @edata dseg segment for data area (without initial value) @einis dseg saddr2 segment for data area ( sreg variable with initial value) @edats dseg saddr2 segment for data area ( sreg variable without initial value) @einis1 dseg saddr segment for data area ( sreg1 with initial value) @edats1 dseg saddr segment for data area ( sreg1 variable without initial value) @ebits bseg sa ddr2 segment for boolean type and bit type variables @ebits1 bseg saddr segment for _ _boolean1 type variable
appendix b list of segment names user?s manual u15556ej1v0um 499 (3) with large model (when ? ? ? ? ml is specified with 2-byte alignment) section name segment type relocation attribute description @ecode cseg segment for normal function codes @ecnst cseg unitp segment for const variables @er_init cseg unitp segment for initialization data (with initial value) @er_inis cseg segment for initialization data ( sreg variable with initial value) @er_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @einit dseg unitp segment for data area (with initial value) @edata dseg unitp segment for data area (without initial value) @einis dseg saddr2 segment for data area ( sreg variable with initial value) @edats dseg saddr2 segment for data area ( sreg variable without initial value) @einis1 dseg saddr segment for data area ( sreg1 with initial value) @edats1 dseg saddr segment for data area ( sreg1 variable without initial value) @ebits bseg sa ddr2 segment for boolean type and bit type variables @ebits1 bseg saddr segment for _ _boolean1 type variable (4) with medium model and location 00h (when ? ? ? ? mm and ? ? ? ? cs0 are specified) section name segment type relocation attribute description @ecode cseg segment for normal function codes @ecnsts cseg base segment for const variables @er_init cseg segment for initialization data (with initial value) @er_inis cseg segment for initialization data ( sreg variable with initial value) @er_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @einit dseg segment for data area (variable with initial value) @edata dseg segment for data area (without initial value) @einis dseg saddr2 segment for data area ( sreg variable with initial value) @edats dseg saddr2 segment for data area ( sreg variable without initial value) @einis1 dseg saddr segment for data area ( sreg1 variable with initial value) @edats1 dseg saddr segment for data area ( sreg1 variable without initial value) @ebits bseg sa ddr2 segment for boolean type and bit type variables @ebits1 bseg saddr segment for _ _boolean1 type variable
appendix b list of segment names user?s manual u15556ej1v0um 500 (5) with medium model and location 0fh (when ? ? ? ? mm and ? ? ? ? cs15 are specified) section name segment type relocation attribute description @ecode cseg segment for normal function codes @ecnstm cseg page64k segment for const variables @er_init cseg segment for initialization data (with initial value) @er_inis cseg segment for initialization data ( sreg variable with initial value) @er_ins1 cseg segment for initialization data ( sreg1 variable with initial value) @einitm dseg page64k segment for data area (with initial value) @edatam dseg page64k segment for data area (without initial value) @einis dseg saddr2 segment for data area ( sreg variable with initial value) @edats dseg saddr2 segment for data area ( sreg variable without initial value) @einis1 dseg saddr segment for data area ( sreg1 variable with initial value) @edats1 dseg saddr segment for data area ( sreg1 variable without initial value) @ebits bseg sa ddr2 segment for boolean type and bit type variables @ebits1 bseg saddr segment for _ _boolean1 type variable b.2 location of segment segment type destination of allocation (default) cseg rom bseg saddr area of ram dseg ram
appendix b list of segment names user?s manual u15556ej1v0um 501 b.3 example of c source #pragma interrupt intp0 inter rb1 /* interrupt vector */ void inter(void); /* interrupt function prototype declaration */ const int i_cnst = 1; /* const variable */ callt void f_clt(void); /* callt function prototype declaration */ callf void f_clf(void); /* callf function prototype declaration */ boolean b_bit; /* boolean type variable */ long l_init = 2; /* external variable with initial value */ int i_data; /* external variable without initial value */ sreg int sr_inis = 3; /* sreg variable with initial value */ sreg int sr_dats; /* sreg variable without initial value */ void main() /* function definition */ { int i; i = 100; } void inter() /* interrupt function definition */ { unsigned char uc = 0; uc++; if(b_bit) b_bit = 0; } callt void f_clt() /* callt function definition */ { } callf void f_clf() /* callf function definition */ { }
appendix b list of segment names user?s manual u15556ej1v0um 502 b.4 example of output assembler module quasi-directives and instruction sets in an assembler source vary depending on the device. refer to the ra78k4 online help for details. ; 78k/iv series c compiler v2.30 assembler source ; date:xx xxx xxxx time:xx:xx:xx ; command : -c4026 sample.c -sa -ng ; in-file : sample.c ; asm-file : sample.asm ; para-file : $chgsfr(15) $processor(4026) $nodebug $nodebuga $kanjicode sjis $tol_inf 03fh, 0230h, 00h, 08021h, 00h public _inter public _i_cnst public ?f_clt public _f_clf public _b_bit public _l_init public _i_data public _sr_inis public _sr_dats public _main public _f_clt public _@vect06 @@bits bseg saddr2 ; segment for boolean type variable _b_bit dbit @@cnst cseg ; segment for const variable _i_cnst: dw 01h ; 1 @@r_init cseg ; segment for initialization data (external variable dw 00002h,00000h ; 2 with initial value) @@init dseg ; segment for data area (external variable with initial _l_init: ds (4) value)
appendix b list of segment names user?s manual u15556ej1v0um 503 @@data dseg ; segment for data area (external variable without _i_data: ds (2) initial value) @@r_inis cseg ; segment for initialization data (sreg variable with dw 03h ; 3 initial value) @@inis dseg saddr2 ; segment for data area (sreg variable with initial _sr_inis: ds (2) value) @@dats dseg saddr2 ; segment for data area (sreg variable without initial _sr_dats: ds (2) value) @@calt cseg callt0 ; segment for callt function ?f_clt: dw _f_clt ; line 1 : #pragma interrupt intp0 inter rb1 /* interrupt vector */ ; line 2 : ; line 3 : void inter(void); /* interrupt function prototype declaration */ ; line 4 : const int i_cnst = 1; /* const variable */ ; line 5 : callt void f_clt(void); /* callt function prototype declaration */ ; line 6 : callf void f_clf(void); /* callf function prototype declaration */ ; line 7 : boolean b_bit; /* boolean type variable */ ; line 8 : long l_init = 2; /* external variable with initial value */ ; line 9 : int i_data; /* external variable without initial value */ ; line 10 : sreg int sr_inis = 3; /* sreg variable with initial value */ ; line 11 : sreg int sr_dats; /* sreg variable without initial value */ ; line 12 : ; line 13 : void main() /* function definition */ ; line 14 : { @@code cseg ; segment for code portion _main: push rp3 ; line 15 : int i; ; line 16 : i = 100; movw rp3,#064h ; 100 ; line 17 : } pop rp3 ret ; line 18 : ; line 19 : void inter() /* interrupt function definition */ ; line 20 : { @@base cseg base ; segment for callf /interrupt function _inter: sel rb1
appendix b list of segment names user?s manual u15556ej1v0um 504 push rp3 ; line 21 : unsigned char uc = 0; mov r6,#00h ; 0 ; line 22 : uc++; inc r6 ; line 23 : if(b_bit) bf _b_bit,$l0005 ; line 24 : b_bit = 0; clr1 _b_bit l0005: ; line 25 : } pop rp3 reti ; line 26 : ; line 27 : callt void f_clt() /* callt function definition */ ; line 28 : { _f_clt: ; line 29 : } ret ; line 30 : ; line 31 : callf void f_clf() /* callf function definition */ ; line 32 : { @@calf cseg fixed ; segment for callf function _f_clf: ; line 33 : } ret @@vect06 cseg at 0006h ; segment for interrupt vector table _@vect06: dw _inter end ; target chip : upd784026 ; device file : vx.xx
user?s manual u15556ej1v0um 505 appendix c list of runtime libraries table c-1 shows the runtime library list. these operational instructions are called in the format where @@, etc. are attached at the beginning of the function name. however, cstart and cstarte are called in the format with _@ attached to the top. all runtime libraries except hdwinit and boot_main are supported when the -zf option is specified. no library support is available for operations not in table c-1. the compiler executes inline expansion. long addition and subtraction, and/or/xor and shift may be expanded inline. table c-1. list of runtime libraries (1/5) classification function name function increment lsinc increments signed long . luinc increments unsigned long . finc increments float . decrement lsdec decrements signed long . ludec decrements unsigned long . fdec decrements float . sign reverse lsrev reverses the sign of signed long . lurev reverses the sign of unsigned long . frev reverses float . complement lscom obtains one?s complement of signed long . lucom obtains one?s complement of unsigned long . not lsnot negates signed long . lunot negates unsigned long . fnot negates float . multiply lsmul performs multiplication between two signed long data. lumul performs multiplication between two unsigned long data. fmul performs multiplication between two float data. divide csdiv performs division between two signed char data. isdiv performs division between two signed int data. lsdiv performs division between two signed long data. ludiv performs division between two unsigned long data. fdiv performs division between two float data. remainder csrem obtains remainder after division between two signed char data. isrem obtains remainder after division between two signed int data. lsrem obtains remainder after division between two signed long data. lurem obtains remainder after division between two unsigned long data.
appendix c list of runtime libraries user?s manual u15556ej1v0um 506 table c-1. list of runtime libraries (2/5) classification function name function add lsadd performs addition between two signed long data. luadd performs addition between two unsigned long data. fadd performs addition between two float data. subtract lssub performs subtraction between two signed long data. lusub performs subtraction between two unsigned long data. fsub performs subtraction between two float data. shift left lslsh shifts signed long to the left. lulsh shifts unsigned long to the left. shift right lsrsh shifts signed long to the right. lursh shifts unsigned long to the right. compare lscmp compares two signed long data. lucmp compares two unsigned long data. fcmp compares two float data. bitwise and lsband performs bitwise and operation between two signed long data. luband performs bitwise and operation between two unsigned long data. bitwise or lsbor performs bitwise or operation between two signed long data. lubor performs bitwise or operation between two unsigned long data. bitwise xor lsbxor performs bitwise xor operation between two signed long data. lubxor performs bitwise xor operation between two unsigned long data. logical and fand performs logical and operation between two float data. logical or for performs logical or operation between two float data. ftols converts from float to signed long. ftolu converts from float to unsigned long. lstof converts from signed long to float . lutof converts from unsigned long to float . type conversion from bit btol converts bit to long . preprocess/ postprocess hdwinit initializes peripheral units ( sfr ) immediately after cpu has been reset. conversion from floating point number conversion to floating point number
appendix c list of runtime libraries user?s manual u15556ej1v0um 507 table c-1. list of runtime libraries (3/5) classification function name function startup routine cstart startup module (including the startup module for booting) z in the case of a startup module for booting , library.inc, in which a library name extern declaration is described in the comments is included. if the library name?s extern declaration comment is removed, it is used in the flash area. the library can be used in the boot area. extern declarations _@vect00 to @vect3e are executed and are located in the flash area. set an interrupt vector table for interrupt functions. secure an area (2 x 32 bytes, 3 x 32 bytes for the medium model and large model) to register functions by the atexit function, and let the top label name be _@fnctbl . secure a break area (32 bytes, 64 bytes in the large model) and let the top label name be _@memtop , then let the area?s next address label name be _@membtm . define the reset vector table?s segment as follows and specify the top address of the startup module. @@vect00 cseg at 0000h dw _@cstart specify location . set the v, u, t and w registers to 0 (small model only). set the v, u, t and w registers to 0 ( location 0 ) and 0fh ( location 15 ) (medium model only). set the register bank to rb0 . set variable _errno input in the error no to 0. set the variable _@fncent which inputs the number of functions registered by the atexit function to. set the address of _@memtop in variable _@brakadr as the initial break value. set 1 as the initial value in the variable _@seed which is the source of pseudo random numbers for the rand function. execute 0 clearing of data from initialization data copy processing and external data without initialization values.
appendix c list of runtime libraries user?s manual u15556ej1v0um 508 table c-1. list of runtime libraries (4/5) classification function name function startup routine cstart startup module (including startup modules for booting) z in the case of a startup module for booting (for flash) call the boot_main function (user program). branch to the flash area?s branch table top ( itbltop ) and move processing to the startup module for flash memory. declare the following labels and variables (distinguish between upper case and lower case letters). the user is prohibited to define these symbols. _@fnctbl (3 bytes: medium model, large model) _@memtop (3 bytes: large model) _@membtm (3 bytes: large model) _errno (2 bytes) _@fncent (2 bytes) _@brkadr (2 bytes/3 bytes: large model) _@seed (4 bytes) _@divr (4 bytes) _@ldivr (8 bytes) _@tokprt (2 bytes/3 bytes: large model) z in the case of a startup module for booting call the main function (user program). call the exit function by parameter 0. cstarte startup module for flash memory define the flash area branch table for branching to the startup module for flash memory ( itbltop is the top address for the flash area branch table). @evect00 cseg at itbltop br _@cstarte set the final address of the stack area + 1 in the stack pointer (sp). execute 0 clearing of data from initialization data copy processing and external data without initialization values. call the main function. call the exit function by parameter 0. flash compatibility boot_main execute boot area main function processing (function prototype: void boot_main (void) ;). this function returns without doing anything. however, as necessary, the user, by creating it, can execute processing which suit?s the user?s purpose. example : in cases where update processing of the flash area program is executed by referring to sfr, etc. vect00 to 3e create an interrupt vector table when the -zf option is specified (function prototype: void vect00(void) ;, ..., void vect3e (void) ). specify the top address value of the interrupt function located in the flash area in the interrupt vector table.
appendix c list of runtime libraries user?s manual u15556ej1v0um 509 table c-1. list of runtime libraries (5/5) classification function name function addwc anda0 ax3de ax3whl axxwhl clrhw cmpa0 cmpax0 cmpaxf cmpbc0 cmpbcf ex2de ex4de mova0 movax1 movaxs movbcf movdes movs0 movsax muluwt muluww mulwde mulwhl sladd slsdiv slsmul slsrem slsub sludiv slumul slurem for replacing the fixed-type instruction pattern auxiliary swtbla converts switch branch table to 2-byte table.
user?s manual u15556ej1v0um 510 appendix d list of library stack consumption table d-1 shows the number of stacks consumed from the standard libraries. table d-1. list of standard library stack consumption (1/4) classification function name small model medium model large model isalnum 0 0 0 salpha 0 0 0 iscntrl 0 0 0 isdigit 0 0 0 isgraph 0 0 0 islower 0 0 0 isprint 0 0 0 ispunct 0 0 0 isspace 0 0 0 isupper 0 0 0 isxdigit 0 0 0 tolower 0 0 0 toupper 0 0 0 isascii 0 0 0 toascii 0 0 0 _tolower 0 0 0 _toupper 0 0 0 tolow 0 0 0 ctype.h toup 0 0 0 setjmp 6 6 0 setjmp.h longjmp 0 0 0 va_arg 0 0 0 va_start 0 0 0 stdarg.h va_end 0 0 0 sprintf 56 (115) 56 (116) 55 (119) note sscanf 293 (334) 293 (335) 293 (341) note printf 65 (116) 67 (118) 71 (121) note scanf 304 (336) 308 (338) 308 (344) note vprintf 65 (116) 67 (118) 71 (121) note vsprintf 56 (115) 56 (116) 55 (119) note getchar 0 0 0 gets 7 7 9 putchar 0 0 0 stdio.h puts 5 5 6 note values in parentheses are for when the version that supports floating-point numbers is used.
appendix d list of library stack consumption user?s manual u15556ej1v0um 511 table d-1. list of standard library stack consumption (2/4) classification function name small model medium model large model atoi 11 11 1 atol 11 11 1 strtol 14 17 21 strtoul 14 17 21 calloc 11 11 18 free 9 9 12 malloc 9 9 12 realloc 14 14 20 abort 0 0 0 atexit 0 0 3 exit n+3 n+3 n+3 note 1 abs 0 0 0 div 6 6 6 labs 0 0 0 ldiv 8 8 11 brk 3 3 6 sbrk 3 3 6 atof 39 39 40 strtod 39 39 40 itoa 6 6 8 ltoa 10 10 12 ultoa 10 10 11 rand 5 5 5 srand 0 0 0 bsearch 25+n 26+n 29+n note 2 qsort 36+n 43+n 44+n note 3 strbrk 3 3 6 strsbrk 3 3 6 stritoa 6 6 8 strltoa 10 10 13 stdlib.h strultoa 10 10 11 notes 1. n is the total stack consumption among external functions registered by the atexit function. 2. n is the stack consumption of external functions called from bsearch . 3. n is (20 + stack consumption of external functions called from qsort ) (1 + number of times recursive calls occurred).
appendix d list of library stack consumption user?s manual u15556ej1v0um 512 table d-1. list of standard library stack consumption (3/4) classification function name small model medium model large model memcpy 0 0 3 memmove 0 0 6 strcpy 0 0 3 strncpy 0 0 3 strcat 0 0 3 strncat 0 0 3 memcmp 0 0 0 strcmp 0 0 0 strncmp 0 0 0 memchr 0 0 0 strchr 0 0 0 strcspn 0 0 3 strpbrk 0 0 3 strrchr 0 0 0 strspn 0 0 3 strstr 2 2 3 strtok 0 0 6 memset 0 0 0 strerror 3 6 6 strlen 0 0 0 strcoll 0 0 0 string.h strxfrm 2 2 3 acos 31 31 31 asin 31 31 31 atan 28 28 28 atan2 28 28 28 cos 26 26 26 sin 26 26 26 tan 33 33 33 cosh 31 31 31 sinh 31 31 31 tanh 37 37 37 exp 28 28 28 frexp 0 (14) 0 (14) 0 (15) note ldexp 0 (11) 0 (11) 0 (12) note log 30 30 30 log10 30 30 30 math.h modf 7 (11) 7 (11) 7 (12) note note values in parentheses are for when an operation exception occurs.
appendix d list of library stack consumption user?s manual u15556ej1v0um 513 table d-1. list of standard library stack consumption (4/4) classification function name small model medium model large model pow 30 30 30 sqrt 12 12 12 ceil 7 (11) 7 (11) 7 (12) note 1 fabs 0 0 0 floor 7 (11) 7 (11) 7 (12) note 1 fmod 6 (11) 6 (11) 6 (12) note 1 matherr 0 0 0 asinf 31 31 31 atanf 28 28 28 atan2f 28 28 28 cosf 26 26 26 sinf 26 26 26 tanf 33 33 33 coshf 31 31 31 sinhf 31 31 31 tanhf 37 37 37 expf 28 28 28 rexpf 0 (14) 0 (14) 0 (15) note 1 ldexpf 0 (11) 0 (11) 0 (12) note 1 logf 30 30 30 log10f 30 30 30 modff 7 (11) 7 (11) 7 (12) note 1 powf 30 30 30 sqrtf 12 12 12 ceilf 7 (11) 7 (11) 7 (12) note 1 fabsf 0 0 0 floorf 7 (11) 7 (11) 7 (12) note 1 math.h fmodf 6 (11) 6 (11) 6 (12) note 1 assert.h _ _assertfail 76 (127) 78 (129) 85 (135) note 2 notes 1. values in parentheses are for when an operation exception occurs. 2. values in parentheses are for when the printf version that supports floating-point numbers is used.
appendix d list of library stack consumption user?s manual u15556ej1v0um 514 table d-2 shows the number of stacks consumed from the runtime libraries. table d-2. list of runtime library stack consumption (1/3) classification function name small model medium model large model lsinc 0 0 0 luinc 0 0 0 increment finc 15 (24) 15 (24) 16 (26) note lsdec 0 0 0 ludec 0 0 0 decrement fdec 15 (24) 15 (24) 16 (26) note lsrev 2 2 2 lurev 2 2 2 sign reverse frev 0 0 0 lscom 0 0 0 1?s complement lucom 0 0 0 lsnot 0 0 0 lunot 0 0 0 logical not fnot 0 0 0 lsmul 2 2 2 lumul 2 2 2 multiply fmul 8 (17) 8 (17) 9 (19) note csdiv 4 4 4 isdiv 6 6 6 lsdiv 13 13 13 ludiv 6 6 6 divide fdiv 8 (17) 8 (17) 9 (19) note csrem 4 4 4 isrem 6 6 6 lsrem 13 13 13 remainder lurem 6 6 6 lsadd 0 0 0 luadd 0 0 0 add fadd 8 (17) 8 (17) 9 (19) note lssub 0 0 0 lusub 0 0 0 subtract fsub 8 (17) 8 (17) 9 (19) note note values in parentheses are for when an operation exception occurs (when the matherr function included with the compiler is used).
appendix d list of library stack consumption user?s manual u15556ej1v0um 515 table d-2. list of runtime library stack consumption (2/3) classification function name small model medium model large model lslsh 0 0 0 shift left lulsh 0 0 0 lsrsh 0 0 0 shift right lursh 0 0 0 lscmp 0 0 0 lucmp 0 0 0 compare fcmp 2 (17) 2 (17) 2 (19) note lsband 0 0 0 bit and luband 0 0 0 lsbor 0 0 0 bit or lubor 0 0 0 lsbxor 0 0 0 bit xor lubxor 0 0 0 logical and fand 0 0 0 logical or for 0 0 0 ftols 2 2 2 conversion from floating-point number ftolu 2 2 2 lstof 2 2 2 conversion to floating-point number lutof 2 2 2 conversion from bit btol 2 2 2 startup routine cstart 3 3 3 note values in parentheses are for when an operation exception occurs (when the matherr function included with the compiler is used).
appendix d list of library stack consumption user?s manual u15556ej1v0um 516 table d-2. list of runtime library stack consumption (3/3) classification function name small model medium model large model addwc 0 5 5 anda0 0 0 0 ax3de 0 0 0 ax3whl 0 0 0 axxwhl 0 0 0 clrhw 0 0 0 cmpa0 0 0 0 cmpax0 0 0 0 cmpaxf 0 0 0 cmpbc0 0 0 0 cmpbcf 0 0 0 ex2de 0 0 0 ex4de 0 0 0 mova0 0 0 0 movax1 0 0 0 movaxs 2 3 5 movbcf 0 0 0 movdes 4 5 5 movs0 4 7 5 movsax 4 7 5 muluwt ?? 0 muluww ?? 0 mulwde ?? 0 mulwhl ?? 0 sladd 3 3 3 note slsdiv 3 3 3 note slsmul 9 9 9 note slsrem 25 25 25 note slsub 5 5 5 note sludiv 9 9 9 note slumul 9 9 9 note slurem 13 11 11 note auxiliary swtbla ?? 0 note note stack correction for the 4 bytes used for placing an argument when a function is called is performed on the side of called function.
user?s manual u15556ej1v0um 517 appendix e index \a...............................................................................35 \b...............................................................................35 \f................................................................................35 \n...............................................................................35 \r................................................................................35 \t................................................................................35 \v ...............................................................................39 #asm - #endasm .....................................................336 #define directive......................................................150 #include ....................................................................50 #include directive ............................144, 145, 146, 147 #operator ................................................................148 ##operator ..............................................................148 #pragma directive ...........................................155, 289 #undef directive ......................................................152 _ _ assertfail ...........................................................278 _ _asm ....................................................................336 _ _boolean ..................................................28, 29, 326 _ _boolean type variables...........................28, 29, 326 _ _boolean1 type variable...........................28, 29, 331 _ _callf ....................................................................356 _ _ callt.....................................................................292 _ _data_ _ ............................................................156 _ _file_ _ ..............................................................156 _ _interrupt..............................................................346 _ _interrupt_brk.......................................................346 _ _line_ _..............................................................156 _ _opc ...................................................................400 _ _pascal_ _ ...............................................29, 31, 421 _ _rtos_interrupt qualifier ........................................408 _ _stdc_ _ ............................................................156 _ _time_ _ .............................................................156 _toupper..................................................................174 -qh option ..............................................................414 -zf option ...............................................................425 -zo option...............................................................413 -zr option ...............................................................424 ??..............................................................................35 a abort........................................................................203 abs ..........................................................................205 absolute address access function ..............28, 30, 363 acos ........................................................................233 acosf .......................................................................256 aggregate type ......................................................... 45 allocation function .................................... 30, 287, 361 ansi ....................................................................... 283 arithmetic operators ................................................. 85 arrays ..................................................................... 128 array type ................................................................. 45 array declarators ...................................................... 59 asin......................................................................... 234 asinf........................................................................ 257 asm statements ......................................... 28, 29, 336 assembly language.................................................. 19 assignment operators............................................. 101 atan ........................................................................ 235 atan2 ...................................................................... 236 atan2f ..................................................................... 259 atanf ....................................................................... 258 atexit....................................................................... 204 atof ......................................................................... 208 atoi ......................................................................... 194 atol ......................................................................... 194 auto .......................................................................... 52 b binary constant................................................. 30, 389 bit field...................................................... 56, 127, 367 bit field declaration ..................................... 28, 30, 367 bit type variables ........................................ 28, 29, 326 bitwise and operators.............................................. 94 bitwise inclusive or operators................................. 96 bitwise xor operators ............................................. 95 block scope .............................................................. 38 boolean type variables ............................... 28, 29, 326 boolean1 type variables ............................. 28, 29, 326 branch statements.................................................. 120 break statements.................................................... 123 brk .......................................................................... 207 brk ........................................................................ 352 bsearch .................................................................. 212 c c language............................................................... 19 callf/_ _callf function................................................. 28 callf function ............................................... 28, 29, 356 calloc ...................................................................... 199
appendix e index user?s manual u15556ej1v0um 518 callt function ..................................................... 28, 292 cast operators .......................................................... 84 ceil.......................................................................... 251 ceilf......................................................................... 274 changing compiler output section name ................ 375 changing function call interface........................ 31, 413 char type .................................................................. 40 character constant ................................................... 48 character type .......................................................... 44 comma operator ..................................................... 104 comment .................................................................. 50 compatible type........................................................ 46 composite type......................................................... 46 compound assignment operators........................... 103 compound statement ............................................. 112 conditional operators.............................................. 100 conditional control statements ............................... 113 const ........................................................................ 58 constants.................................................................. 46 constant expressions ............................................. 105 continue statement................................................. 122 cos ......................................................................... 237 cosf ........................................................................ 260 cosh ....................................................................... 240 coshf ...................................................................... 263 cpu control instruction .................................... 30, 352 d data insertion function................................ 28, 31, 400 decimal constant ...................................................... 47 delimiters.................................................................. 49 di ........................................................................... 349 div .......................................................................... 206 device type............................................................. 156 division function ......................................... 28, 30, 398 do statement .......................................................... 118 e ei ........................................................................... 349 enumeration constant .............................................. 48 enumeration specifiers............................................. 56 enumeration type ..................................................... 41 equality operators .................................................... 91 escape sequence..................................................... 35 exit ......................................................................... 204 exp ......................................................................... 243 expf ........................................................................ 266 expression statements ...........................................112 ext_tsk ....................................................................410 extern ...............................................................52, 134 external object definitions.......................................136 external linkage ........................................................39 external definitions .................................................133 f fabs.........................................................................252 fabsf........................................................................275 file scope ..................................................................38 firmware rom function...........................................433 flash area branch table...........................................426 floating point constant ..............................................47 floating point type .....................................................41 floor ........................................................................253 floorf .......................................................................276 fmod........................................................................254 fmodf.......................................................................277 for statement...........................................................119 free .........................................................................200 frexp .......................................................................244 frexpf ......................................................................267 function.....................................................................23 function call function from the boot area ................430 function declarators ..................................................60 function definition ...................................................134 function prototype scope ..........................................38 function scope ..........................................................38 function to change compiler output section name ....30 function type .............................................................45 g general integral promotion........................................67 getchar ...................................................................190 gets.........................................................................191 goto statement........................................................121 h halt ......................................................................352 header file...............................................................163 header name ............................................................50 hexadecimal constant...............................................47 i identifiers ..................................................................37
appendix e index user?s manual u15556ej1v0um 519 if...else statement....................................................114 incomplete type.........................................................45 integer type ...............................................................67 integral type ..............................................................41 internal linkage..........................................................39 interrupt function qualifier .......................................347 interrupt functions .............................................29, 340 interrupt handler for rtos ...............................31, 402 interrupt handler qualifier for rtos..................31, 408 isalnum ...................................................................171 isalpha ....................................................................171 isascii ......................................................................171 iscntrl ......................................................................171 isdigit.......................................................................171 isgraph ....................................................................171 islower.....................................................................171 isprint ......................................................................171 ispunct ....................................................................171 isspace....................................................................171 isupper ....................................................................171 isxdigit.....................................................................171 iteration statement ..................................................116 itoa ..........................................................................210 k key words..................................................................36 l labeled statements..................................................109 labs .........................................................................205 ldexp .......................................................................245 ldexpf ......................................................................268 ldiv ..........................................................................206 log ...........................................................................246 log10 .......................................................................247 log10f ......................................................................270 logf ..........................................................................269 logical and operators...............................................98 logical or operators .................................................99 longjmp ...................................................................175 ltoa ..........................................................................210 m machine language ....................................................19 macro name............................................................156 macro replacement directives.................................150 malloc .....................................................................201 matherr ................................................................... 255 memchr .................................................................. 222 memcmp................................................................. 220 memcpy.................................................................. 217 memmove............................................................... 217 memset................................................................... 228 medium model................................................ 287, 358 modf ....................................................................... 248 modff ...................................................................... 271 module name changing function ...................... 30, 391 multiplication function................................. 28, 30, 395 n noauto functions......................................... 28, 29, 312 no linkage................................................................. 39 nop........................................................................ 352 norec functions........................................... 28, 29, 318 o octal constant ........................................................... 47 p pascal function ................................................. 31, 421 pascal function call interface .................................. 424 peekb ..................................................................... 363 peekw..................................................................... 363 pintf ........................................................................ 186 pointer ...................................................................... 69 pointer declarator ..................................................... 59 pokeb ..................................................................... 363 pokew..................................................................... 363 postfix operators....................................................... 73 pow......................................................................... 249 powf........................................................................ 272 preprocessing directives ........................................ 137 putchar ................................................................... 192 puts ........................................................................ 193 q qsort ....................................................................... 213 r rand ........................................................................ 211 realloc..................................................................... 202 re-entrantability ...................................................... 169 register ..................................................................... 52
appendix e index user?s manual u15556ej1v0um 520 register bank .......................................................... 287 register bank specification ..................................... 341 register variables...................................................... 28 relational operators .................................................. 90 return statement..................................................... 124 rolb ......................................................................... 392 rolw ........................................................................ 392 romization-related section name.......................... 383 rorb......................................................................... 392 rorw ........................................................................ 392 rotate function ............................................ 28, 30, 392 rtos ..................................................................... 283 s scalar type............................................................... 45 sbrk ........................................................................ 207 scanf ...................................................................... 187 setjmp .................................................................... 175 sfr area..................................................................... 29 sfr variable ............................................................. 309 shift operators .......................................................... 88 signed integral type.................................................. 41 simple assignment operators ................................. 102 sin .......................................................................... 238 sinf ......................................................................... 261 sinh ........................................................................ 241 sinhf ....................................................................... 264 small model.................................................... 287, 358 sprintf ..................................................................... 178 sqrt ......................................................................... 250 sqrtf ........................................................................ 273 srand ...................................................................... 211 sreg declaration ..................................................... 301 sreg variable .................................................... 28, 301 sreg1 variable ........................................................ 306 sscanf..................................................................... 182 stack change specification ..................................... 342 startup routine ........................................................ 384 static................................................................. 52, 134 stop ..................................................................... 352 storage class specifiers............................................ 52 strbrk ...................................................................... 214 strcat ...................................................................... 219 strchr ...................................................................... 223 strcmp .................................................................... 221 strcoll...................................................................... 231 strcpy ..................................................................... 218 strcspn ................................................................... 224 strerror ....................................................................229 string literals .............................................................49 stritoa......................................................................216 strlen.......................................................................230 strltoa......................................................................216 strncat.....................................................................219 strncmp...................................................................221 strncpy....................................................................218 strpbrk ....................................................................225 strrchr .....................................................................223 strsbrk.....................................................................215 strspn......................................................................224 strstr .......................................................................226 strtod ......................................................................208 strtol........................................................................227 strtoul......................................................................196 struct.......................................................................126 structures................................................................126 structure pointer .....................................................126 structure specifier .....................................................55 structure type............................................................45 structure variable....................................................126 strultoa....................................................................216 strxfrm.....................................................................232 switch statement.....................................................115 t tags...........................................................................57 tan ..........................................................................239 tanf .........................................................................262 tanh ........................................................................242 tanhf .......................................................................265 task.........................................................................410 task function for rtos .....................................31, 410 toascii .....................................................................173 tolow .......................................................................174 tolower ....................................................................172 toup ........................................................................174 toupper ...................................................................172 trigraph sequences...................................................35 type conversions ......................................................65 type names ...............................................................60 type qualifiers ...........................................................58 typedef......................................................................52 u ultoa........................................................................210
appendix e index user?s manual u15556ej1v0um 521 unary operators.........................................................79 union .......................................................................130 union specifier...........................................................55 union type .................................................................45 unsigned integral type...............................................41 usage of saddr area................................................301 v va_arg.....................................................................176 va_end ....................................................................176 va_start ...................................................................176 va_starttop ..............................................................176 void ...........................................................................69 void pointer ...............................................................69 volatile.......................................................................58 vprintf ......................................................................188 vsprintf ....................................................................189 w while statement.......................................................117
522 [memo]
although nec has taken all possible steps to ensure that the documentation supplied to our customers is complete, bug free and up-to-date, we readily accept that errors may occur. despite all the care and precautions we've taken, you may encounter problems in the documentation. please complete this form whenever you'd like to report errors or suggest improvements to us. hong kong, philippines, oceania nec electronics hong kong ltd. fax: +852-2886-9022/9044 korea nec electronics hong kong ltd. seoul branch fax: +82-2-528-4411 taiwan nec electronics taiwan ltd. fax: +886-2-2719-5951 address north america nec electronics inc. corporate communications dept. fax: +1-800-729-9288 +1-408-588-6130 europe nec electronics (europe) gmbh market communication dept. fax: +49-211-6503-274 south america nec do brasil s.a. fax: +55-11-6462-6829 asian nations except philippines nec electronics singapore pte. ltd. fax: +65-250-3583 japan nec semiconductor technical hotline fax: +81- 44-435-9608 i would like to report the following error/make the following suggestion: document title: document number: page number: thank you for your kind support. if possible, please fax the referenced page or drawing. excellent good acceptable poor document rating clarity technical accuracy organization cs 01.11 name company from: tel. fax facsimile message


▲Up To Search▲   

 
Price & Availability of USBB17CC78K4

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X